前言
對著《Java 編程思想》,通過wait - notifyAll實現了生產者消費者模式。今天用BlockingQueue實現一下。
BlockingQueue
簡單實現
生產者和消費者,共用一個BlockingQueue。為什么BlockingQueue能夠實現生產者-消費者模型呢?對于put
和take
兩個操作,注釋如下:
/**
* Inserts the specified element into this queue, waiting if necessary
* for space to become available.
*
* @param e the element to add
* @throws InterruptedException if interrupted while waiting
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this queue
*/
void put(E e) throws InterruptedException;
/**
* Retrieves and removes the head of this queue, waiting if necessary
* until an element becomes available.
*
* @return the head of this queue
* @throws InterruptedException if interrupted while waiting
*/
E take() throws InterruptedException;
Apple.java,生產和消費的對象。
public class Apple {
private int id;
public Apple(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "Apple [id=" + id + "]";
}
}
生產者:
public class Producer {
BlockingQueue<Apple> queue;
public Producer(BlockingQueue<Apple> queue) {
this.queue = queue;
}
public boolean put(Apple apple) {
return queue.offer(apple);
}
}
消費者:
public class Consumer {
BlockingQueue<Apple> queue;
public Consumer(BlockingQueue<Apple> queue) {
this.queue = queue;
}
public Apple take() throws InterruptedException {
return queue.take();
}
}
測試:
public class TestConsumer {
public static void main(String[] args) {
final BlockingQueue<Apple> queue = new LinkedBlockingDeque<Apple>(100);
// 生產者
new Thread(new Runnable() {
int appleId = 0;
Producer producer = new Producer(queue);
@Override
public void run() {
try {
while (true) {
TimeUnit.SECONDS.sleep(1);
producer.put(new Apple(appleId++));
producer.put(new Apple(appleId++));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
// 消費者
new Thread(new Runnable() {
Consumer consumer = new Consumer(queue);
@Override
public void run() {
try {
while (true) {
System.out.println(consumer.take().getId());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
輸出:
生產者生產2個Apple,消費者立即消費掉。
改進
上述代碼存在一些問題:
- 生產者和消費者,都僅用于特定的類型
Apple
- 在使用過程中,需要自己定義BlockingQueue,自行實現生產者和消費者的線程,使用復雜
- 如果要定義多個消費者線程,需要多次手動編寫代碼
- 生產者并沒有專注自身的功能:存儲要消費的對象
- 消費者并沒有專注自身的功能:取出對象、如何消費對象
改進后的代碼如下:
Apple類未更改。
Producer變為抽象類,并使用泛型。里面新增線程池
,用于運行消費者線程。
public abstract class Producer<E> {
protected BlockingQueue<E> queue;
protected ExecutorService threadPool = Executors.newCachedThreadPool();
public static final int DEFAULT_QUEUE_LENGTH = 10000;
public Producer(int capacity) {
initQueue(capacity);
}
public BlockingQueue<E> getQueue() {
return queue;
}
public void setQueue(BlockingQueue<E> queue) {
this.queue = queue;
}
public boolean put(E apple) {
return queue.offer(apple);
}
private void initQueue(int capacity) {
if (queue == null) {
synchronized (this) {
if (queue == null) {
queue = new LinkedBlockingDeque<E>(capacity < 0 ? DEFAULT_QUEUE_LENGTH : capacity);
}
}
}
}
protected void consumerThread(int consumerCount, Consumer<E> consumer) {
for (int i = 0; i < consumerCount; i++) {
threadPool.execute(consumer);
}
}
}
Consumer也變成抽象類,使用泛型,并實現了Runnable接口。其中run方法的實現邏輯是:從阻塞隊列中取出一個對象,并調用抽象方法consume
。該方法是具體的消費者實現的消費邏輯。
public abstract class Consumer<E> implements Runnable{
BlockingQueue<E> queue;
/**
* 數據逐個處理
* @param data
*/
protected abstract void consume(E data);
@Override
public void run() {
while (true) {
try {
E data = take();
try {
consume(data);
} catch (Exception e) {
e.printStackTrace();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public Consumer(BlockingQueue<E> queue) {
this.queue = queue;
}
public E take() throws InterruptedException {
return queue.take();
}
}
AppleProducer:Apple的生產者,使用非延遲加載的單例模式,指定阻塞隊列的長度、消費者線程數量。
public class AppleProducer extends Producer<Apple>{
// 并沒有延遲加載
public static AppleProducer INSTANCE = new AppleProducer(DEFAULT_QUEUE_LENGTH, 1);
private AppleProducer(int capacity, int consumerCount) {
super(capacity);
AppleConsumer consumer = new AppleConsumer(queue);
consumerThread(consumerCount, consumer);
}
}
AppleConsumer:Apple的消費者,要實現具體的消費方法consume
。這里只是在控制臺輸出對象信息。
public class AppleConsumer extends Consumer<Apple>{
public AppleConsumer(BlockingQueue<Apple> queue) {
super(queue);
}
@Override
protected void consume(Apple data) {
System.out.println(data);
}
}
測試:這里只需要獲取AppleProducer,調用put方法添加對象即可!在隊列中有對象Apple時,會有線程取出Apple,自動調用AppleConsumer的consume方法。
public class TestConsumer {
public static void main(String[] args) throws InterruptedException {
AppleProducer producer = AppleProducer.INSTANCE;
for (int i = 0; i < 60; i++) {
producer.put(new Apple(i));
}
}
}
有待改進的地方
- 并沒有面向接口編程,仍然是通過繼承來實現的,代碼有耦合(但是也不能算是缺點吧)
- 阻塞隊列直接使用LinkedBlockingDeque,并不夠靈活(PriorityBlockingQueue等)
- 對于線程,并沒有好的名字,調試等并不直觀
- 如果有多個生產者-消費者,例如增加了Banana,管理仍然不夠直觀。可以增加一個方法,能夠打印出所有的生產者-消費者