public class TestProduct {
public static void main(String[] args) {
Warehouse warehouse = new Warehouse(10);//建立一个仓库,容量为10
//建立生产者线程和消费者
Producer producers1 = new Producer(warehouse,"producer-1");
Producer producers2 = new Producer(warehouse,"producer-2");
Producer producers3 = new Producer(warehouse,"producer-3");
Consumer consumer1 = new Consumer(warehouse,"consumer-1");
Consumer consumer2 = new Consumer(warehouse,"consumer-2");
Consumer consumer3 = new Consumer(warehouse,"consumer-3");
Consumer consumer4 = new Consumer(warehouse,"consumer-4");
//启动生产者线程和消费者线程
producers1.start();
producers2.start();
consumer1.start();
producers3.start();
consumer2.start();
consumer3.start();
consumer4.start();
//让生产者/消费者程序运行1600ms
try {
Thread.sleep(1600);
} catch (InterruptedException e) {
e.printStackTrace();
}
//停止消费者线程
producers1.stopProducer();
consumer1.stopConsumer();
producers2.stopProducer();
consumer2.stopConsumer();
producers3.stopProducer();
consumer3.stopConsumer();
consumer4.stopConsumer();
}
}
输出结果: