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

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

    導航

    統計

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 麻豆亚洲av熟女国产一区二| 亚洲av无码专区青青草原| 国产成人精品免费视频动漫 | 国产三级免费观看| 国产精品免费αv视频| 亚洲精品成人久久| 国产伦精品一区二区三区免费下载 | 亚洲国产人成在线观看| 日韩免费三级电影| 久久青青草原国产精品免费| 最新国产成人亚洲精品影院| 亚洲人精品午夜射精日韩| 最新仑乱免费视频| 在线观看免费无码专区| 亚洲AV无码一区二区大桥未久| 国产成人精品日本亚洲网站| 成人免费无码大片a毛片| 国产午夜免费高清久久影院| 国产成人综合亚洲绿色| 亚洲福利秒拍一区二区| 国产亚洲精品成人a v小说| 妞干网在线免费视频| 91精品手机国产免费| 又长又大又粗又硬3p免费视频 | 无码精品人妻一区二区三区免费 | 美女视频黄a视频全免费网站色窝| 亚洲人成7777| 亚洲福利视频一区| 国产亚洲视频在线播放| 真实乱视频国产免费观看| 亚洲网站免费观看| 精品一区二区三区免费| 免费无遮挡无遮羞在线看| 亚洲六月丁香婷婷综合| 亚洲天堂中文资源| 亚洲人成在线播放网站| 亚洲人成网站观看在线播放 | 国产.亚洲.欧洲在线| 中文字幕亚洲综合久久| 亚洲va久久久噜噜噜久久天堂| 亚洲综合亚洲综合网成人|