<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)

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲视频精品在线| 免费大片黄手机在线观看| 综合亚洲伊人午夜网| 国产AV日韩A∨亚洲AV电影| 久久久久久久免费视频| 亚洲国产电影在线观看| 99re在线免费视频| 亚洲精品亚洲人成在线麻豆| 免费A级毛片无码A∨中文字幕下载| 精品国产_亚洲人成在线高清| 免费一级毛片在线播放放视频| 免费乱码中文字幕网站| 国产亚洲视频在线观看| 亚洲成a人一区二区三区| 免费手机在线看片| 国产亚洲老熟女视频| 中文字幕av免费专区| 久久亚洲国产视频| 4399影视免费观看高清直播| 亚洲人成在久久综合网站| 野花高清在线电影观看免费视频| 亚洲AV无码精品蜜桃| 国产网站免费观看| gogo免费在线观看| 亚洲综合视频在线| 一二三四免费观看在线视频中文版| 亚洲6080yy久久无码产自国产| 亚洲日韩国产成网在线观看| 免费无码H肉动漫在线观看麻豆| 亚洲高清视频在线观看| 91精品视频免费| 国产成人 亚洲欧洲| 狠狠综合久久综合88亚洲| 最近免费中文字幕大全高清大全1| 亚洲国产精品成人精品软件 | 国产成人免费ā片在线观看老同学| 亚洲国产精品成人精品无码区在线| 67194国产精品免费观看| 亚洲精华国产精华精华液好用 | 中文字幕乱码一区二区免费| 亚洲国产成人精品电影|