一種實(shí)現(xiàn)生產(chǎn)者-消費(fèi)者問題的新方法
生產(chǎn)者-消費(fèi)者問題,是操作系統(tǒng)的進(jìn)程管理中很重要的問題。以前的我都是用wait(),notify()方法來實(shí)現(xiàn)的。今天我在翻閱JDK1.5API幫助文檔的時(shí)候,偶然間發(fā)現(xiàn)了一個(gè)類ArrayBlockingQueue,它其中提供了以下兩個(gè)方法:
void |
put (E o) Adds the specified element to the tail of this queue, waiting if necessary for space to become available. |
E |
take() Retrieves and removes the head of this queue, waiting if no elements are present on this queue. |
我靈機(jī)一動(dòng),用這兩個(gè)JDK中定義好的方法是不是也可以實(shí)現(xiàn)生產(chǎn)者-消費(fèi)者問題呢?試了一下,嘿,還真靈。


















































































































posted on 2007-03-25 14:17 我為J狂 閱讀(2055) 評(píng)論(3) 編輯 收藏 所屬分類: 線程