問題描述
假設(shè)分別有4位廚師和6位食客。
廚師做一盤菜的時(shí)間是4S,食客吃一盤菜的時(shí)間是3S。
每位廚師做好菜后放入有固定容量(10盤)的桌子上。
如果廚師做好菜發(fā)現(xiàn)桌子上已經(jīng)有10盤菜了,就必須等待任意一個(gè)食客吃掉一盤后才能放入;
如果食客取菜時(shí)發(fā)現(xiàn)桌子上沒有菜,也必須等待有任一廚師做好菜放入桌子才能取用。
代碼:
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) { // 向容器內(nèi)放置食品
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+",現(xiàn)在桌上有"+this.size()+"份菜**");
notifyAll();
}
public synchronized Food getFood() { // 從容器內(nèi)取食品
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+",現(xiàn)在桌上有"+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 {
} // 食品類
運(yùn)行結(jié)果:
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,現(xiàn)在桌上有1份菜**
吃了一份jsp_ex2.Food@783e9d8b,現(xiàn)在桌上有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,現(xiàn)在桌上有1份菜**
上了一份jsp_ex2.Food@b468cdf,現(xiàn)在桌上有2份菜**
上了一份jsp_ex2.Food@302702c3,現(xiàn)在桌上有3份菜**
吃了一份jsp_ex2.Food@30681e13,現(xiàn)在桌上有2份菜
吃了一份jsp_ex2.Food@b468cdf,現(xiàn)在桌上有1份菜
吃了一份jsp_ex2.Food@302702c3,現(xiàn)在桌上有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,現(xiàn)在桌上有1份菜**
吃了一份jsp_ex2.Food@76f96cfc,現(xiàn)在桌上有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,現(xiàn)在桌上有1份菜**
上了一份jsp_ex2.Food@3eaff66e,現(xiàn)在桌上有2份菜**
吃了一份jsp_ex2.Food@58163c7,現(xiàn)在桌上有1份菜
吃了一份jsp_ex2.Food@3eaff66e,現(xiàn)在桌上有0份菜
There is no food now ,come here later!
There is no food now ,come here later!
上了一份jsp_ex2.Food@3ee0fab7,現(xiàn)在桌上有1份菜**
吃了一份jsp_ex2.Food@3ee0fab7,現(xiàn)在桌上有0份菜
There is no food now ,come here later!
There is no food now ,come here later!
程序沒寫菜名,簡單實(shí)現(xiàn)....
因篇幅問題不能全部顯示,請(qǐng)點(diǎn)此查看更多更全內(nèi)容
Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2
違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)