阻塞隊列和生產者-消費者模式

          自Java5以來提供的BlockingQueue是一種特殊的隊列, 它 是支持兩個附加操作的 Queue,這兩個操作是:檢索元素時等待隊列變?yōu)榉强眨约按鎯υ貢r等待空間變得可用。

          以JDK中的例子略加改寫如下

          1 import java.util.concurrent.ArrayBlockingQueue;
          2 import java.util.concurrent.BlockingQueue;
          3
          4 class Producer implements Runnable {
          5     private final BlockingQueue queue;
          6
          7      Producer(BlockingQueue q) {
          8          queue = q;
          9      }
          10
          11     public void run() {
          12         try {
          13             while (true) {
          14                  queue.put(produce());
          15              }
          16          } catch (InterruptedException ex) {
          17              System.out.println("produce interrupted " + ex.getMessage());
          18              Thread.currentThread().interrupt();
          19             //return;
          20          }
          21      }
          22
          23      Object produce() {
          24          System.out.println("produce laugh");
          25         return "haha";
          26      }
          27 }
          28
          29 class Consumer implements Runnable {
          30     private final BlockingQueue queue;
          31
          32      Consumer(BlockingQueue q) {
          33          queue = q;
          34      }
          35
          36     public void run() {
          37         try {
          38             while (true) {
          39                  consume(queue.take());
          40              }
          41          } catch (InterruptedException ex) {
          42              System.out.println("consume interrupted " + ex.getMessage());
          43              Thread.currentThread().interrupt();
          44          }
          45      }
          46
          47     void consume(Object x) {
          48          System.out.println("consume laugh "+ x);
          49      }
          50 }
          51
          52 public class BlockingQueueTest {
          53     public static void main(String args[]) {
          54          BlockingQueue q = new ArrayBlockingQueue(10);
          55          Producer p = new Producer(q);
          56          Consumer c1 = new Consumer(q);
          57          Consumer c2 = new Consumer(q);
          58          Thread pTh = new Thread(p);
          59          pTh.start();
          60          Thread cTh1 = new Thread(c1);
          61          cTh1.start();
          62          Thread cTh2 = new Thread(c2);
          63          cTh2.start();
          64         try {
          65              Thread.sleep(3000);
          66          }catch (Exception e) {
          67             // TODO: handle exception
          68          }
          69          pTh.interrupt();
          70          cTh1.interrupt();
          71          cTh2.interrupt();
          72      }
          73 }
          74

          posted on 2008-07-26 23:42 fantasyamin 閱讀(1090) 評論(0)  編輯  收藏 所屬分類: Java

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統(tǒng)計

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 伊宁县| 凤庆县| 东台市| 江北区| 丁青县| 张家界市| 武夷山市| 和政县| 阆中市| 宁夏| 玛沁县| 平湖市| 漳州市| 府谷县| 东台市| 扶余县| 多伦县| 无棣县| 莒南县| 常熟市| 溧阳市| 疏勒县| 麦盖提县| 贞丰县| 黄梅县| 金乡县| 遂昌县| 台中县| 翁源县| 桓台县| 灵寿县| 监利县| 达拉特旗| 安顺市| 新竹市| 安阳县| 托里县| 盘山县| 丹阳市| 潍坊市| 沁阳市|