问题描述
假设分别有4位厨师和6位食客。
厨师做一盘菜的时间是4S,食客吃一盘菜的时间是3S。
每位厨师做好菜后放入有固定容量(10盘)的桌子上。
如果厨师做好菜发现桌子上已经有10盘菜了,就必须等待任意一个食客吃掉一盘后才能放入;
如果食客取菜时发现桌子上没有菜,也必须等待有任一厨师做好菜放入桌子才能取用。
代码:
Test类:
public class Test{
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
Table t=new Table(10);
new Cook(t).start();
new Cook(t).start();
new Cook(t).start();
new Cook(t).start();
new Diners(t).start();
new Diners(t).start();
new Diners(t).start();
new Diners(t).start();
new Diners(t).start();
new Diners(t).start();
}
}
Table类:
import java.util.LinkedList;
@SuppressWarnings("serial")
class Table extends LinkedList {
int maxSize; // 容器的最大容量
public Table(int maxSize) {
this.maxSize = maxSize;
}
public synchronized void putFood(Food f) { // 向容器内放置食品
while (this.size() >= this.maxSize) {
try {
System.out.println("The table is too full,wait a moment!");
wait();
} catch (Exception e) {
}
}
this.addLast(f);
System.out.println("上了一份"+f+",现在桌上有"+this.size()+"份菜**");
notifyAll();
}
public synchronized Food getFood() { // 从容器内取食品
Food f;
while (this.size() <= 0) {
try {
System.out.println("There is no food now ,come here later!");
wait();
} catch (Exception e) {
}
}
f = (Food) this.getFirst();
this.remove(f);
System.out.println("吃了一份"+f+",现在桌上有"+this.size()+"份菜");
notifyAll();
return f;
}
}
Cook类
public class Cook extends Thread {
private Table t;
Cook( Table t) {
this.t = t;
}
public void run() {
while (true) {
Food f = name();
try{
Thread.sleep(4000);
}catch(InterruptedException e){}
t.putFood(f);
}
}
private Food name() {
// TODO Auto-generated method stub
return new Food();
}
}
Diners类:
public class Diners extends Thread {
private Table t = new Table(getPriority());
Diners(Table t){
this.t=t;
}
public void run(){
while(true){
Food f=t.getFood();
try{
Thread.sleep(3000);
}catch(InterruptedException e){}
}
}
}
Food类:
class Food {
} // 食品类
运行结果:
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
上了一份jsp_ex2.Food@783e9d8b,现在桌上有1份菜**
吃了一份jsp_ex2.Food@783e9d8b,现在桌上有0份菜
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
上了一份jsp_ex2.Food@30681e13,现在桌上有1份菜**
上了一份jsp_ex2.Food@b468cdf,现在桌上有2份菜**
上了一份jsp_ex2.Food@302702c3,现在桌上有3份菜**
吃了一份jsp_ex2.Food@30681e13,现在桌上有2份菜
吃了一份jsp_ex2.Food@b468cdf,现在桌上有1份菜
吃了一份jsp_ex2.Food@302702c3,现在桌上有0份菜
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
上了一份jsp_ex2.Food@76f96cfc,现在桌上有1份菜**
吃了一份jsp_ex2.Food@76f96cfc,现在桌上有0份菜
There is no food now ,come here later!
There is no food now ,come here later!
There is no food now ,come here later!
上了一份jsp_ex2.Food@58163c7,现在桌上有1份菜**
上了一份jsp_ex2.Food@3eaff66e,现在桌上有2份菜**
吃了一份jsp_ex2.Food@58163c7,现在桌上有1份菜
吃了一份jsp_ex2.Food@3eaff66e,现在桌上有0份菜
There is no food now ,come here later!
There is no food now ,come here later!
上了一份jsp_ex2.Food@3ee0fab7,现在桌上有1份菜**
吃了一份jsp_ex2.Food@3ee0fab7,现在桌上有0份菜
There is no food now ,come here later!
There is no food now ,come here later!
程序没写菜名,简单实现....