Java中实现Consumer-Producer

问题描述:一个仓库可以存放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 例外。

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值