包子卖完了啊 2016-09-07 10:10 采纳率: 100%
浏览 1071
已采纳

一个简单的生产者与消费者问题

问题描述:有一个这样的饭店,他有一个厨师和服务员。这个服务员必须等待厨师准备好膳食。当厨师准备好时,他会通知服务员,之后服务员上菜,然后返回继续等待。
代码如下
package concurrency;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

class Meal{

private final int id;

public Meal(int id){
    this.id = id;
}

public String toString(){
    return "Meal: " + id;
}

}

class WaitMan implements Runnable{

private Restaurant restaurant;

public WaitMan(Restaurant restaurant){
    this.restaurant = restaurant;
}

public void run() {
    try {
        while (!Thread.interrupted()) {
            synchronized (this) {
                while (restaurant.meal == null) {
                    wait();
                }
            }
            System.out.println("Waitman got " + restaurant.meal);
            synchronized (restaurant.chef) {
                restaurant.meal = null;
                restaurant.chef.notifyAll();
            }
        }
    } catch (InterruptedException e) {
        System.out.println("WaitMan intettupted");
    }
}

}

class Chef implements Runnable{

private Restaurant restaurant;
private int count = 0;

public Chef(Restaurant restaurant){
    this.restaurant = restaurant;
}

public void run(){
    try {
        while(!Thread.interrupted()){
            synchronized (this) {
                while(restaurant.meal != null){
                    wait();
                }   
            }
        }
        if(++count == 10){
            System.out.println("Out of food, closing");
            restaurant.exec.shutdownNow();
            System.exit(0);
        }
        System.out.println("Order up!");
        synchronized (restaurant.waitMan) {
            restaurant.meal = new Meal(count);
            restaurant.waitMan.notifyAll();
        }
        TimeUnit.MILLISECONDS.sleep(100);
    } catch (InterruptedException e) {
        System.out.println("Chef intettupted");
    }
}

}

public class Restaurant {
Meal meal;
WaitMan waitMan = new WaitMan(this);
Chef chef = new Chef(this);
ExecutorService exec = Executors.newCachedThreadPool();
public Restaurant(){
exec.execute(chef);
exec.execute(waitMan);
}
public static void main(String[] args) {
new Restaurant();
}

}
代码运行不出结果,貌似出现了死锁,求大神赐教

  • 写回答

1条回答

  • DreamerJ 2016-09-07 12:58
    关注

    Chef里面的while循环结束)的位置放错了吧,应该是

    public void run(){
        try {
            while(!Thread.interrupted()){
                synchronized (this) {
                    while(restaurant.meal != null){
                        wait();
                    }   
                }
                            if(++count == 10){
                                  System.out.println("Out of food, closing");
                                  restaurant.exec.shutdownNow();
                                  System.exit(0);
                          }
                            System.out.println("Order up!");
                synchronized (restaurant.waitMan) {
                    restaurant.meal = new Meal(count);
                    restaurant.waitMan.notifyAll();
                }
                TimeUnit.MILLISECONDS.sleep(100);
            }
        } catch (InterruptedException e) {
            System.out.println("Chef intettupted");
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置