package com.itheima.threaddemo14;
public class Demo {
public static void main(String[] args) {
Foodie f=new Foodie();
Cooker c=new Cooker();
f.start();
c.start();
}
}
package com.itheima.threaddemo14;
public class Foodie extends Thread{
@Override
public void run() {
while (true){
synchronized (Desk.lcok){
if(Desk.count==0){
break;
} else{
//有
if (Desk.flag){
System.out.println("吃货在吃汉堡包");
Desk.flag=false;
Desk.lcok.notifyAll();
Desk.count--;
}else {
try {
Desk.lcok.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
package com.itheima.threaddemo14;
public class Cooker extends Thread{
@Override
public void run() {
while (true){
synchronized (Desk.lcok){
if (Desk.count==0){
break;
}else {
if (!Desk.flag){
//生产
System.out.println("厨师生产汉堡包");
Desk.flag=true;
Desk.lcok.notifyAll();
}else {
try {
Desk.lcok.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
}
}
package com.itheima.threaddemo14;
public class Desk {
//是否有汉堡包
public static boolean flag=false;
//汉堡包的总数量
public static int count=10;
//锁对象
public static final Object lcok= new Object();
}