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!

程序没写菜名,简单实现....

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值