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();
}
}
}
}
黑马程序员——多个生产者,多个消费者 notifyAll() 方式二
最新推荐文章于 2021-06-04 23:46:47 发布