BlockingQueue實現生產者-消費者模式

來源:互聯網
上載者:User

生產者-消費者模式是以緩衝區為協作的橋樑,生產者只需要負責生產,並把生產的訊息放入緩衝區,不用管有多少生產者和消費者,而消費者只需要負責取出緩衝區裡的訊息來消費,也不需要管有多少消費者和生產者。

而BlockingQueue很好的解決了如何將一個線程收集的訊息傳遞給另一線程用於處理的問題,並且不需要考慮同步問題。

下面為代碼的實現:

 1 /** 2  *  3  */ 4 package bells; 5  6 import java.util.Arrays; 7 import java.util.List; 8 import java.util.concurrent.ArrayBlockingQueue; 9 import java.util.concurrent.BlockingQueue;10 11 /**12  * 生產者13  * 14  * @author bellszhu15  * 16  */17 class Procuder implements Runnable {18 19     private BlockingQueue<String> queue;20 21     public Procuder(BlockingQueue<String> queue) {22         this.queue = queue;23     }24 25     @Override26     public void run() {27         try {28             for (String message : produceMessages()) {29                 queue.put(message);30             }31         } catch (InterruptedException e) {32             System.err.println("Interrupted! " + e.getMessage());33         }34     }35 36     /**37      * 簡單實現生產過程38      * 39      * @return40      */41     private List<String> produceMessages() {42         return Arrays.asList("I'm coming", "I'm done it", "I'm win",43                 "congratulate", "over");44     }45 }46 47 /**48  * 消費者49  * 50  * @author bellszhu51  * 52  */53 class Consumer implements Runnable {54 55     private BlockingQueue<String> queue;56 57     public Consumer(BlockingQueue<String> queue) {58         this.queue = queue;59     }60 61     @Override62     public void run() {63         try {64             String message = null;65             while (!((message = queue.take()).equals("over"))) {66                 // 消費者處理訊息67                 System.out.println("Thread: " + Thread.currentThread().getId()68                         + "\tmessage: " + message);69             }70         } catch (InterruptedException e) {71             System.err.println("Interrupted! " + e.getMessage());72         }73     }74 }75 76 /**77  * @author bellszhu78  * 79  */80 public class ProcuderConsumerTest {81     public static void main(String[] args) {82         BlockingQueue<String> queue = new ArrayBlockingQueue<String>(1, true);83 84         new Thread(new Procuder(queue)).start();85 86         new Thread(new Consumer(queue)).start();87         new Thread(new Consumer(queue)).start();88     }89 }

 關於生產者-消費者模式的設計可以參考:http://blog.csdn.net/program_think/article/details/4022087

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.