/**
* 生产一个,消费一个;
* 等待唤醒机制(单生产者单消费者)
* 选作(能看懂最后的包子案例即可)
*/
public class Prictice2 {
public static void main(String[] args) throws InterruptedException {
// 创建一个集合容器
ArrayList<String> list = new ArrayList<>();
In in = new In(list);
Out out = new Out(list);
// 创建线程对象
Thread thread1 = new Thread(in,"商家");
Thread thread2 = new Thread(out,"买家");
// 间接启动线程
thread1.start();
Thread.sleep(2000);
thread2.start();
while(true) {
Thread.sleep(1000);
System.out.println("------------------------包子总数:"+list.size()+"个");
}
}
}
In类:
package cn.itcast.day27_Prictice;
import java.util.ArrayList;
public class In implements Runnable {
// 声明一个私有属性
private ArrayList<String> list;
private int count;