您好,欢迎来到九壹网。
搜索
您的当前位置:首页java多线程厨师做饼,Java多线程之厨师与食客问题

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!

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

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 91gzw.com 版权所有 湘ICP备2023023988号-2

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务