package pojo.xc01;
public class Cook extends Thread{
@Override
public void run() {
while (true) {
synchronized (Desk.lock) {
if (Desk.count == 0) break;
if (Desk.foodFlog == 1) {//桌子上有食物,就等待
try {
Desk.lock.wait();
} catch (Exception e) {
e.printStackTrace();
}
} else { //没有食物,就制作食物
Desk.foodFlog = 1;
System.out.println("做好了");
Desk.lock.notifyAll();//唤醒消费者开吃
}
}
}
}
}
----------------------------------------------------------------------
public static void main(String[] args) throws Exception {
Cook cook = new Cook();
Foodie foodie = new Foodie();
cook.start();
foodie.start();
}
标签:Java,生产者,lock,Desk,Cook,唤醒,public From: https://www.cnblogs.com/gagaya2/p/17794079.html