问题描述:一个仓库可以存放N件物品。生产者每生产一件产品,将产品放入仓库,仓库满了就停止生产。消费者每次从仓库中去一件物品,然后进行消费,仓库空时就停止消费。
解答:在J2SE1.5中的java.util.concurrent 新功能包提供一些实用的通用类供并行程序使用。其中的BlockingQueue接口及其实现类对于解决[生产者-消费者问题]非常方便。下面是实现的代码(例子摘抄自jdk1.5帮助文档,稍做修改):
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* @author Rebirth
*/
class Consumer implements Runnable {
private int i=0;
private String sName="";
private final BlockingQueue queue;
Consumer(BlockingQueue q,String sName) { queue = q;this.sName=sName; }
/*
* @see java.lang.Runnable#run()
*/
public void run() {
while(i<5) {
try{
//消费产品
consume(queue.take());
}
catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}
finally{i++;}
}
}
private void consume(Object product){
System.out.println(sName + " consume " + product.toString());
}
}
class Producer implements Runnable {
private int i=0;
private String sName="";
private int iNum=0;
private final BlockingQueue queue;
Producer(BlockingQueue q,String sName) { queue = q;this.sName=sName; }
/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
while(i<10) {
try{
//生产产品
queue.put(produce());
}catch (InterruptedException ex) {
System.out.println(ex.getMessage());
}finally{i++;}
}
}
private Object produce(){
String sProduct ="p" + (++iNum);
System.out.println(sName + " product " + sProduct);
return sProduct;
}
}
public class Factory {
public static void main(String[] args) {
BlockingQueue q = new ArrayBlockingQueue(10);
Producer p = new Producer(q," p1 ");
Consumer c1 = new Consumer(q," c1 ");
Consumer c2 = new Consumer(q," c2 ");
new Thread(p).start();
new Thread(c1).start();
new Thread(c2).start();
}
}
BlockingQueue是线程安全的,当仓库中已经没有物品而消费者继续往里面取时队列自动进行阻塞。需要注意的是,BlockingQueue是不接受null值的,但尝试将一个null值插入队列,会抛出NullPointerException 例外。
凡是有该标志的文章,都是该blog博主Caoer(草儿)原创,凡是索引、收藏
、转载请注明来处和原文作者。非常感谢。