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