用java写的生产者消费者问题的源代码及解释

/**
 * @function 提供了生产者消费者问题的源代码
 * @author 郑路路
 * @time 2013/3/16
 */

public class ProblemTest {
 public static void main(String[] args){
  BreadStack bs = new BreadStack();    //得到一个面包框的对象
  Producer p = new Producer(bs);       //得到一个生产者对象
  Consumer c = new Consumer(bs);       //得到一个消费者对象
  //有几个生产者线程就对应几个消费者线程
  new Thread(p).start();
  new Thread(p).start();
  new Thread(c).start();
  new Thread(c).start();
 }
}
/*
 * 定义面包的类,给每个面包一个编号
 */
class Bread{
 private int id;
 Bread(int id){
  this.id = id;
 }
 
 public String toString(){
  return "bread: " + id;
 }
}
/*
 * 定义面包框的类
 */
class BreadStack{
 private int index = 0;
 private Bread[] breads = new Bread[6];
 
 public Bread[] getBreads() {
  return breads;
 }
 public void setBreads(Bread[] breads) {
  this.breads = breads;
 }
 public int getIndex() {
  return index;
 }
 public void setIndex(int index) {
  this.index = index;
 }
 public synchronized void push(Bread b){
  while(index == breads.length){
   try {
    //在等待之前先唤醒在此对象监视器上等待的所有线程
    this.notifyAll();          
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   
  }
  breads[index] = b;
  index ++;
 }
 public synchronized Bread pop(){
  while(index == 0){
   try {
    //在等待之前先唤醒在此对象监视器上等待的所有线程
    this.notifyAll();         
    this.wait();
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   
  }
  index --;
  return breads[index];
 }
}
/*
 * 定义生产者的类
 */
class Producer implements Runnable{
 private BreadStack bs = null;
 
 Producer(BreadStack bs) {
  this.bs = bs;
 }
 public void run() {
  //一个生产者最多只能生产20个馒头
  for(int i=0; i<20; i++){
   Bread bread = new Bread(i);
   bs.push(bread);
   System.out.println("生产了" + bread);
   //生产一个馒头之后休息1s钟的时间
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
}
/*
 * 定义消费者的类
 */
class Consumer implements Runnable{
 private BreadStack bs = null;
 
 Consumer(BreadStack bs) {
  this.bs = bs;
 }
 public void run() {
  //一个生产者最多只能消费20个馒头
  for(int i=0; i<20; i++){
   Bread bread = bs.pop();
   System.out.println("消费了" + bread);
   //消费一个馒头之后休息1s钟的时间
   try {
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
  }
 }
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值