//测试类
public class A {
public static void main(String[] args) {
CakeShop cakeShop = new CakeShop();
//10个消费者线程
for(int i = 0;i<10;i++){
new Thread(new Sell(cakeShop)).start();
}
//5个生产者线程
for(int i = 0;i<5;i++){
new Thread(new Produce(cakeShop)).start();
}
}
}
运行结果:
class CakeShop{
int num = 0;
Lock lock = new ReentrantLock();
Condition fullCondition = lock.newCondition();//蛋糕过多条件
Condition emptyCondition = lock.newCondition();//没有蛋糕条件
public static void main(String[] args) {
CakeShop cakeShop = new CakeShop();
//10个消费者线程
for(int i = 0;i<10;i++){
new Thread(new Sell(cakeShop)).start();
}
//5个生产者线程
for(int i = 0;i<5;i++){
new Thread(new Produce(cakeShop)).start();
}