<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电影| 国产亚洲精品精华液| 亚洲一线产区二线产区精华| 91精品啪在线观看国产线免费| 亚洲AV无码一区东京热| 久久精品视频免费播放| 亚洲国产女人aaa毛片在线| 最近2019免费中文字幕6| 亚洲成av人片不卡无码| 好男人视频社区精品免费| 亚洲综合色一区二区三区| 国产无遮挡吃胸膜奶免费看 | 在线jyzzjyzz免费视频| 亚洲综合一区国产精品| 午夜亚洲av永久无码精品| 日韩精品免费一线在线观看| 亚洲中文字幕伊人久久无码| 久草免费福利视频| 亚洲精品成人久久| 成年私人影院免费视频网站| 国产亚洲精彩视频| 亚洲精品二区国产综合野狼| 一个人免费日韩不卡视频| 亚洲粉嫩美白在线| 亚洲成人高清在线| 一级特黄aa毛片免费观看| 亚洲一线产品二线产品| 亚洲黄片手机免费观看| 久久永久免费人妻精品下载| 亚洲成a人片在线看| 又粗又硬又大又爽免费视频播放| 999zyz**站免费毛片| 亚洲偷自精品三十六区| 亚洲国产精品狼友中文久久久| 免费观看91视频| 亚洲日韩中文字幕一区| 国产精品亚洲产品一区二区三区 | 91久久青青草原线免费| 国产综合成人亚洲区| 亚洲人成电影福利在线播放 | 亚洲黄网在线观看|