阻塞隊(duì)列和生產(chǎn)者-消費(fèi)者模式

          自Java5以來提供的BlockingQueue是一種特殊的隊(duì)列, 它 是支持兩個(gè)附加操作的 Queue,這兩個(gè)操作是:檢索元素時(shí)等待隊(duì)列變?yōu)榉强?,以及存?chǔ)元素時(shí)等待空間變得可用。

          以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

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 承德县| 东至县| 江源县| 赤城县| 安福县| 沙雅县| 澜沧| 石门县| 拜泉县| 兴仁县| 永泰县| 夏河县| 杭锦旗| 台前县| 南郑县| 冀州市| 江孜县| 车险| 霍州市| 临沭县| 揭西县| 图片| 邯郸县| 义乌市| 石台县| 望江县| 交城县| 韩城市| 尉氏县| 沂南县| 手机| 龙游县| 两当县| 盘山县| 泸定县| 汪清县| 龙州县| 外汇| 奉贤区| 南溪县| 冀州市|