2. 仿真练习, 模拟生产者和消费者问题。
题目:.厨师和消费者之间有一张桌子。厨师每做出一道菜,就把菜放到桌子上。 消费者如果座子上有菜,就每次拿一道菜拿去吃,如果桌子上没有菜,就等待。 已知桌子上最多只能容纳10道菜,若桌子放满菜,厨师就休息等待一段时间。请在控制台上模拟这一活动。
模拟过程如下:
1.厨师每做出一道菜,就在控制台打印:”the chef make a deal”.
2.消费者每拿走一道菜, 就在控制台打印: “the consumer takes away one deal”
3.厨师生产一道菜和消费者每消费一道菜都需要一段时间, 具体时间可自由设置。
这一活动可以通过线程睡觉来模拟。提示:调用线程的sleep函数。
4.当桌子菜已放满, 厨师就休息一段时间,再工作,休息时间可自由设置。此时控制台打印:”the desk is full, and the chef want to relax”.
5.当桌子上没有菜时,消费者就休息一段时间,休息时间可自由设置。 此时控制台打印:”the desk is empty, the consumer is complaining”.
麻烦哪位高手写出代码,谢谢了,我是初学者,这是我作业...
题目:.厨师和消费者之间有一张桌子。厨师每做出一道菜,就把菜放到桌子上。 消费者如果座子上有菜,就每次拿一道菜拿去吃,如果桌子上没有菜,就等待。 已知桌子上最多只能容纳10道菜,若桌子放满菜,厨师就休息等待一段时间。请在控制台上模拟这一活动。
模拟过程如下:
1.厨师每做出一道菜,就在控制台打印:”the chef make a deal”.
2.消费者每拿走一道菜, 就在控制台打印: “the consumer takes away one deal”
3.厨师生产一道菜和消费者每消费一道菜都需要一段时间, 具体时间可自由设置。
这一活动可以通过线程睡觉来模拟。提示:调用线程的sleep函数。
4.当桌子菜已放满, 厨师就休息一段时间,再工作,休息时间可自由设置。此时控制台打印:”the desk is full, and the chef want to relax”.
5.当桌子上没有菜时,消费者就休息一段时间,休息时间可自由设置。 此时控制台打印:”the desk is empty, the consumer is complaining”.
麻烦哪位高手写出代码,谢谢了,我是初学者,这是我作业...
解决方案 »
- 管道的容量多大?
- layout的问题,左侧3个面板以垂直方向排列,中间俩个面板,以垂直方向排列,右侧1个面板,用什么Layout
- 用JMF实现屏幕录象的问题
- java中如何限制输出数字
- 实在解决不了这个语法错误了。帮帮忙吧!
- 高难.如何让一个HttpServletResponse能forward两次? 或通过一个HttpServletResponse生成另一个HttpServletResponse
- TextField的对齐问题(菜鸟级问题)
- B/S结构中报表的处理
- 如何通过JDB:ODBC访问局域网中的其他机器上的MYSQL数据库(在线等待)?
- Java初学者请教:多线程生产者消费者中不释放锁为依然何正常运行?
- android sqlite 中文乱码问题
- 用ssh框架怎么查询所有
class Chef extends Thread {
private final int INTERVAL = 100; private Table table = null; public Chef(Table table) {
this.table = table;
} public void run() {
while (true) {
try {
if (table.getMealNum() >= 10) {
System.out
.println("the desk is full, and the chef want to relax");
Thread.sleep(INTERVAL);
} else {
table.incMeal();
System.out.println("the chef make a deal");
Thread.sleep(INTERVAL);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}class Consumer extends Thread {
private final int INTERVAL = 100;
private Table table = null;
public Consumer(Table table) {
this.table = table;
}
public void run() {
while (true) {
try {
if (table.getMealNum() == 0) {
System.out.println("the desk is empty, the comsumer is complaining");
Thread.sleep(INTERVAL);
} else {
table.decMeal();
System.out.println("the consumer takes away one meal");
Thread.sleep(INTERVAL);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
class Table {
private Integer mealNum = 0; public Table() {
} public void incMeal() {
synchronized (mealNum) {
mealNum++;
}
} public void decMeal() {
synchronized (mealNum) {
mealNum--;
}
} public int getMealNum() {
synchronized (mealNum) {
return mealNum;
}
}
}public class Main { public static void main(String[] args) {
Table table = new Table();
Chef chef = new Chef(table);
Consumer consumer = new Consumer(table);
chef.start();
consumer.start();
}
}
import java.util.concurrent.Semaphore;public class ConCurrentTest {
private int MAX_CONTAIN = 20;
private final Semaphore sam = new Semaphore(1);
Random random = new Random();
private Object lock = new Object();
private int max = 0; public ConCurrentTest() {
try{
sam.acquire();
}catch(Exception e){
}
}
public void eat() {
try {
Thread.sleep(random.nextInt(2) * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (max == 0)
System.out.println("顾客[" + Thread.currentThread().getName()
+ "]-->没饭吃了,oh my god!");
try {
sam.acquire();
Thread.sleep(50);
} catch (Exception e) { }
synchronized (lock) {
max--;
System.out.println("顾客[" + Thread.currentThread().getName()
+ "]-->疯狂的啃食中!");
}
}
public void cook() {
try {
Thread.sleep(random.nextInt(5) * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (max == MAX_CONTAIN) {
System.out.println("厨师[" + Thread.currentThread().getName()
+ "]--->做太多了,休息一下!");
try {
Thread.sleep(random.nextInt(10) * 1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
sam.release();
synchronized (lock) {
max++;
System.out.println("厨师[" + Thread.currentThread().getName()
+ "]--->疯狂的make中!");
}
}
class Cooker implements Runnable {
public void run() {
while (true)
cook();
}
} class Consumer implements Runnable { public void run() {
while (true)
eat();
}
} public void test() {
Thread th1 = new Thread(new Consumer());
th1.setName("SpiderMan");
th1.start();
Thread th2 = new Thread(new Cooker());
th2.setName("SuperMan");
th2.start();
} public static void main(String[] args) {
new ConCurrentTest().test();
}
}信号量的,允许多个 厨师,多个顾客
信号量是好东西 就写过C的 回去试试Java的