包子卖完了啊 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 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误