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

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

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

    瘋狂

    STANDING ON THE SHOULDERS OF GIANTS
    posts - 481, comments - 486, trackbacks - 0, articles - 1
      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

    concurren套件(轉載)

    Posted on 2009-11-02 15:04 瘋狂 閱讀(255) 評論(0)  編輯  收藏 所屬分類: java

    從J2SE 5.0開始有了java.util.concurrent套件,其中的類可以使實現多線程相關功能更為方便。本節將簡介concurrent套件中的幾個簡單常用的類。

    15.3.1 BlockingQueue

    隊列(Queue)是一個先進先出(First In First Out, FIFO)的數據結構。在J2SE 5.0中增加了java.util.concurrent.BlockingQueue。在多線程情況下,如果BlockingQueue的內容為空,而有個線程試圖從Queue中取出元素,則該線程會被Block,直到Queue有元素時才解除Block;反過來,如果 BlockingQueue滿了,而有個線程試圖再把數據填入Queue中,則該線程會被Block,直到Queue中有元素被取走后解除Block。

    BlockingQueue的幾個主要操作如表15-1所示。

    表15-1  BlockingQueue的幾個操作

     

     

    java.util.concurrent中提供幾種不同的BlockingQueue。ArrayBlockingQueue要指定容量大小來構建。LinkedBlockingQueue默認沒有容量上限,但也可以指定容量上限。PriorityBlockingQueue嚴格來說不是Queue,因為它是根據優先權(Priority)來移除元素。

    我們以在wait()、notify()介紹時的生產者、消費者程序為例,使用BlockQueue來加以改寫,優點是不用親自處理wait()、notify()的細節。首先生產者改寫如范例15.21所示:

    范例15.21  ProducerQueue.java

     

    package onlyfun.caterpillar;
    import java.util.concurrent.BlockingQueue;
    public class ProducerQueue implements Runnable {
                private BlockingQueue<Integer> queue;
        public ProducerQueue(BlockingQueue<Integer> queue) {
                this.queue = queue;
                }
        public void run() {
                for(int product = 1; product <= 10; product++) {
                try {
                // wait for a random time
                Thread.sleep((int) Math.random() * 3000);
                queue.put(product);
                }
                catch(InterruptedException e) {
                e.printStackTrace();
                }
                }
                }
                }

    可以看到,直接使用BlockingQueue,會自動處理同步化、wait()和notify()的執行。消費者類改寫如范例15.22所示:

    范例15.22  ConsumerQueue.java

     

    package onlyfun.caterpillar;
    import java.util.concurrent.BlockingQueue;
    public class ConsumerQueue implements Runnable {
                private BlockingQueue<Integer> queue;
        public ConsumerQueue(BlockingQueue<Integer> queue) {
                this.queue = queue;
                }
        public void run() {
                for(int i = 1; i <= 10; i++) {
                try {
                // wait for a random time
                Thread.sleep((int) (Math.random() * 3000));
                queue.take();
                }
                catch(InterruptedException e) {
                e.printStackTrace();
                }
                }
                }
                }

    可以使用范例15.23進行簡單的測試:

    范例15.23  BlockingQueueDemo.java

     

    package onlyfun.caterpillar;
    import java.util.concurrent.BlockingQueue;
    public class ConsumerQueue implements Runnable {
                private BlockingQueue<Integer> queue;
        public ConsumerQueue(BlockingQueue<Integer> queue) {
                this.queue = queue;
                }
        public void run() {
                for(int i = 1; i <= 10; i++) {
                try {
                // 等待一個隨機時間
                Thread.sleep((int) (Math.random() * 3000));
                queue.take();
                }
                catch(InterruptedException e) {
                e.printStackTrace();
                }
                }
                }
                }

    由于BlockingQueue不需要您來控制,所以沒有特意顯示信息以表示生產者、消費者放入產品至Queue的信息,不過仍可以在ProducerQueue與ConsumerQueue中放入相關信息顯示,以確認程序確實在運轉。

    主站蜘蛛池模板: 中文字幕av无码不卡免费| 美女羞羞免费视频网站| 久热免费在线视频| 国产亚洲情侣一区二区无| caoporn成人免费公开| 亚洲精品无码久久毛片| 人成电影网在线观看免费| 免费看小12萝裸体视频国产| 色吊丝性永久免费看码| 亚洲精品国产自在久久| 中国毛片免费观看| 亚洲A∨无码无在线观看| 在线观看的免费网站无遮挡| 亚洲精品乱码久久久久久下载| 69xx免费观看视频| 最新亚洲精品国偷自产在线| 青青青国产免费一夜七次郎| eeuss影院ss奇兵免费com| 久久精品国产亚洲一区二区| 99精品一区二区免费视频| 亚洲va成无码人在线观看| 在线观看免费污视频| 青青青视频免费观看| 亚洲国产婷婷六月丁香| 99视频免费观看| 亚洲国产精华液2020| 亚洲一区无码精品色| 亚洲毛片在线免费观看| 亚洲欧洲精品成人久久曰| 在线精品亚洲一区二区小说| 18禁免费无码无遮挡不卡网站| 亚洲中文无码亚洲人成影院| 亚洲精品无码AV中文字幕电影网站 | 免费h视频在线观看| 亚洲成人黄色网址| 国产在线ts人妖免费视频| 你懂的免费在线观看网站| 亚洲第一男人天堂| 亚洲色成人中文字幕网站| 一二三四视频在线观看中文版免费| 看免费毛片天天看|