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”.
麻烦哪位高手写出代码,谢谢了,我是初学者,这是我作业...

解决方案 »

  1.   

    我以前也没怎么做过这类型的  就当练练手 - -  作业还是得自己做啊代码如果有错 大虾请指正啊
    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();
    }
    }
      

  2.   

    代码写的很好,真是感激不尽。不过,就是运行的时候,好半天才出来一个”the desk is empty, the consumer is complaining”.,我把时间改了一下,就好多了。谢谢你啦!
      

  3.   

    package com.ssj.testcase;import java.util.Random;
    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();
    }
    }信号量的,允许多个 厨师,多个顾客
      

  4.   


    信号量是好东西  就写过C的  回去试试Java的