多线程作业
有一个厨师负责生产馒头,放在一个容量为10的篮子里,有3个学生负责吃馒头。
当篮子已经装满的时候厨师要暂停生产,当篮子为空的时候学生暂停吃馒头,
要求:使用多线程模拟该过程。(需要线程同步)
1.Consumer类
class Consumer implements Runnable {
SyncStack ss = null;
Consumer(SyncStack ss) {
this.ss = ss;
}
public void run() {
for(int i=0; i<20; i++) {
MaTou wt = ss.pop();
//System.out.println(Thread.currentThread().getName()+"消费了: " + wt);
try {
//Thread.sleep((int)(Math.random() * 1000));
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2.Producer类
class Producer implements Runnable {
SyncStack ss = null;
Producer(SyncStack ss) {
this.ss = ss;
}
public