<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    自Java5以來提供的BlockingQueue是一種特殊的隊列, 它 是支持兩個附加操作的 Queue,這兩個操作是:檢索元素時等待隊列變為非空,以及存儲元素時等待空間變得可用。

    以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 閱讀(1091) 評論(0)  編輯  收藏 所屬分類: Java

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

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久亚洲综合色一区二区三区| 国外成人免费高清激情视频| 青青草原亚洲视频| 四虎国产精品永免费| 免费国产在线观看老王影院| 色妞www精品视频免费看| 国产精品高清全国免费观看| 亚洲AV无码AV吞精久久| 亚洲A∨午夜成人片精品网站| 日产久久强奸免费的看| 亚洲国产精品VA在线看黑人 | 在线看亚洲十八禁网站| 啊灬啊灬别停啊灬用力啊免费看| 美女又黄又免费的视频| 中文字幕亚洲乱码熟女一区二区 | 视频免费在线观看| 亚洲综合自拍成人| 国内精品乱码卡1卡2卡3免费| 亚洲人成在线中文字幕| 日本久久久免费高清| 免费一级毛片在线播放放视频| 亚洲日本一区二区三区在线| 无码av免费一区二区三区| 亚洲免费视频网址| 免费永久在线观看黄网站| 免费国产污网站在线观看| 亚洲日本视频在线观看| 四虎www免费人成| A国产一区二区免费入口| 中文字幕亚洲第一在线| 好爽…又高潮了毛片免费看| 一区二区在线免费视频| 337p日本欧洲亚洲大胆色噜噜| 在线免费观看一区二区三区| 一区二区三区在线观看免费| 亚洲黄色网址在线观看| 国产精品无码免费视频二三区| 国产成人无码区免费网站| 亚洲精品亚洲人成在线播放| 亚洲午夜精品一级在线播放放| 最近2019中文字幕免费直播|