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

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

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 久久精品国产亚洲av麻豆蜜芽 | 亚洲视频免费在线观看| 亚洲自偷自偷在线成人网站传媒| 亚洲精品无码久久久影院相关影片| 在线观看免费成人| 天天影院成人免费观看| 国精产品一区一区三区免费视频 | 在线成人a毛片免费播放 | 亚洲国产精品成人精品小说| 久久精品国产亚洲av成人| 亚洲一区二区三区无码影院| 亚洲丶国产丶欧美一区二区三区| 久久久亚洲欧洲日产国码aⅴ| 亚洲午夜久久久影院| 亚洲国产V高清在线观看| 手机看片久久国产免费| 色妞WWW精品免费视频| 特级毛片全部免费播放| 亚洲AV永久无码天堂影院| 亚洲乱码在线卡一卡二卡新区| 亚洲首页在线观看| 亚洲国产精品婷婷久久| 亚洲成AV人片在线观看WWW| 久久精品亚洲福利| 国产精品亚洲综合一区| 免费又黄又爽又猛的毛片| 久久青草91免费观看| a级成人毛片免费图片| 国产精品综合专区中文字幕免费播放| 特级毛片全部免费播放a一级| 黄色网址在线免费观看| 无遮挡呻吟娇喘视频免费播放| 美女18毛片免费视频| 特级毛片aaaa级毛片免费| 成人午夜免费视频| 狠狠躁狠狠爱免费视频无码| 久久久久久国产a免费观看不卡 | 免费亚洲视频在线观看| 免费一级国产生活片| 久久精品国产亚洲7777| 亚洲人色婷婷成人网站在线观看 |