黑马程序员——多个生产者,多个消费者 notifyAll() 方式二

               ----------------------     android培训      java培训   期待与您交流!    ----------------------   
                        

package thread;

public class Procedure_Customer_Up{
 public static void main(String[] args) {
  Resource_up res=new Resource_up();
  
  Procedure_up pro=new Procedure_up(res);
  Procedure_up pro1=new Procedure_up(res);
  
  Customer_up cus=new Customer_up(res);
  Customer_up cus1=new Customer_up(res);
 
  new Thread(pro).start();
  new Thread(pro1).start();
  new Thread(cus).start();
  new Thread(cus1).start();
 }
}
class Resource_up{
 private String name;
 private int count=1;
 private boolean boo;
// public synchronized void setNameCount(String name){
//  while(boo){
//    try{
//     this.wait();
//    }catch(Exception e){
//     e.printStackTrace();
//    }
//  }
//  this.name=name+",,,"+count++;
//  System.out.println("生产者:"+this.name);
//  boo=true;
//  this.notifyAll();
// }
// public synchronized void out(){
//  while(!boo){
//   try{
//    this.wait();
//   }catch(Exception e){
//    e.printStackTrace();
//   }
//  }
//  
//  System.out.println("============消费者======"+this.name);
//  boo=false;
//  this.notifyAll();
// }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name+"..."+count++;
 }
 public int getCount() {
  return count;
 }
 public void setCount(int count) {
  this.count = count;
 }
 public boolean isBoo() {
  return boo;
 }
 public void setBoo(boolean boo) {
  this.boo = boo;
 }
 
}
class Procedure_up implements Runnable{
 private Resource_up res;
 public Procedure_up(Resource_up res){
  this.res=res;
 }
 public void run(){
  while(true){
   synchronized(res){
    while(res.isBoo()){
     try{
      res.wait();
     }catch(Exception e){};
    }
    res.setName("abc");
    System.out.println("生产者:"+res.getName());
    res.setBoo(true);
    res.notifyAll();
   }
  }
 }
}
class Customer_up implements Runnable{
 private Resource_up res;
 public Customer_up(Resource_up res){
  this.res=res;
 }
 public void run(){
  while(true){
   synchronized(res){
    while(!res.isBoo()){
     try{
      res.wait();
     }catch(Exception e){}
    }
    System.out.println("消费者:::::::::"+res.getName());
    res.setBoo(false);
    res.notifyAll();
   }
  }
 }
}

               ----------------------     android培训      java培训   期待与您交流!    ----------------------   
                         详细请查看       http://edu.csdn.net/heima

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King·Forward

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值