java多线程厨师做饼,Java多线程之厨师与食客问题
問題描述
假設分別有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!
程序沒寫菜名,簡單實現....
總結
以上是生活随笔為你收集整理的java多线程厨师做饼,Java多线程之厨师与食客问题的全部內容,希望文章能夠幫你解決所遇到的問題。