如果你想避免使用錯綜復雜的
wait
–notify的
語句,BlockingQueue非常有用。BlockingQueue
可用於解決生產者-消費者問題,如下代碼示例。對於每個開發人員來說,生產者消費者問題已經非常熟悉了,這裡我將不做詳細描述。
BlockingQueue
適合解決生產者消費者問題任何有效的生產者-消費者問題解決方案都是通過控制生產者put()方法(生產資源)和消費者take()方法(消費資源)的調用來實現的,一旦你實現了對方法的阻塞控制,那麼你將解決該問題。
Java通過BlockingQueue
提供了開箱即用的支持來控制這些方法的調用(一個線程創建資源,另一個消費資源)。java.util.concurrent
包下的BlockingQueue
接口是一個線程安全的可用於存取對象的隊列。
BlockingQueue是一種數據結構,支持一個線程往裡存資源,另一個線程從裡取資源。這正是解決生產者消費者問題所需要的,那麼讓我們開始解決該問題吧。
以下代碼用於生產者線程
package cn.edu.hdu.chenpi.cpdemo.howtodoinjava; import java.util.concurrent.BlockingQueue; class Producer implements Runnable { protected BlockingQueue<Object> queue; Producer(BlockingQueue<Object> theQueue) { this.queue = theQueue; } public void run() { try { while (true) { Object justProduced = getResource(); queue.put(justProduced); System.out.println("Produced resource - Queue size now = " + queue.size()); } } catch (InterruptedException ex) { System.out.println("Producer INTERRUPTED"); } } Object getResource() { try { Thread.sleep(100); // simulate time passing during read } catch (InterruptedException ex) { System.out.println("Producer Read INTERRUPTED"); } return new Object(); } }
這裡,生產者線程創建一個資源(Object對象)並將它存入隊列中,如果隊列已經滿了(最大為20),它將會等待直到消費者線程從隊列中取走資源,所以隊列的長度永遠不會超過最大值,比如20。
以下代碼用於消費者線程
package cn.edu.hdu.chenpi.cpdemo.howtodoinjava; import java.util.concurrent.BlockingQueue; class Consumer implements Runnable { protected BlockingQueue<Object> queue; Consumer(BlockingQueue<Object> theQueue) { this.queue = theQueue; } public void run() { try { while (true) { Object obj = queue.take(); System.out.println("Consumed resource - Queue size now = " + queue.size()); take(obj); } } catch (InterruptedException ex) { System.out.println("CONSUMER INTERRUPTED"); } } void take(Object obj) { try { Thread.sleep(100); // simulate time passing } catch (InterruptedException ex) { System.out.println("Consumer Read INTERRUPTED"); } System.out.println("Consuming object " + obj); } }
隊列中有資源的話,消費者線程將從隊列中取走資源,否則會等待直到生產者創建資源。
現在,讓我們測試下以上生產者消費者線程是否能正常工作:
package cn.edu.hdu.chenpi.cpdemo.howtodoinjava; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class ProducerConsumerExample { public static void main(String[] args) throws InterruptedException { int numProducers = 4; int numConsumers = 3; BlockingQueue<Object> myQueue = new LinkedBlockingQueue<>(20); for (int i = 0; i < numProducers; i++) { new Thread(new Producer(myQueue)).start(); } for (int i = 0; i < numConsumers; i++) { new Thread(new Consumer(myQueue)).start(); } // Let the simulation run for, say, 10 seconds Thread.sleep(10 * 1000); // End of simulation - shut down gracefully System.exit(0); } }
當我們運行以上代碼,會發現類似如下輸出結果:
Consumed resource - Queue size now = 1 Produced resource - Queue size now = 1 Consumed resource - Queue size now = 1 Consumed resource - Queue size now = 1 Produced resource - Queue size now = 1 Produced resource - Queue size now = 1 Produced resource - Queue size now = 1 Consuming object java.lang.Object@14c7f728 Consumed resource - Queue size now = 0 Consuming object java.lang.Object@2b71e323 Consumed resource - Queue size now = 0 Produced resource - Queue size now = 0 Produced resource - Queue size now = 1 Produced resource - Queue size now = 2 Consuming object java.lang.Object@206dc00b Consumed resource - Queue size now = 1 Produced resource - Queue size now = 2 Produced resource - Queue size now = 3 Consuming object java.lang.Object@1a000bc0 Consumed resource - Queue size now = 2 Consuming object java.lang.Object@25b6183d Consumed resource - Queue size now = 1 Produced resource - Queue size now = 2 Produced resource - Queue size now = 3 ... ... Produced resource - Queue size now = 20 Consuming object java.lang.Object@2b3cd3a6 Consumed resource - Queue size now = 19 Produced resource - Queue size now = 20 Consuming object java.lang.Object@3876982d Consumed resource - Queue size now = 19 Produced resource - Queue size now = 20
從輸出結果中,我們可以發現隊列大小永遠不會超過20,消費者線程消費了生產者生產的資源。
譯文鏈接:http://howtodoinjava.com/algorithm/producer-consumer-problem-using-blockingqueue/