一種實現生產者-消費者問題的新方法
生產者-消費者問題,是操作系統的進程管理中很重要的問題。以前的我都是用wait(),notify()方法來實現的。今天我在翻閱JDK1.5API幫助文檔的時候,偶然間發現了一個類ArrayBlockingQueue,它其中提供了以下兩個方法:
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. |
我靈機一動,用這兩個JDK中定義好的方法是不是也可以實現生產者-消費者問題呢?試了一下,嘿,還真靈。


















































































































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