前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 生产者消费者实现

Java 生产者消费者实现

作者头像
Yano_nankai
发布2018-10-08 10:38:10
8940
发布2018-10-08 10:38:10
举报
文章被收录于专栏:二进制文集二进制文集

《Java 编程思想》中,使用 厨师-服务员 来表示:生产者、消费者。

思路

chef 在 有 meal 的时候,会释放锁;在制作 meal 时,会获取 waitPerson 的锁,制作完 meal 后,唤醒所有的 waitPerson;

waitPerson 在 没有 meal 的时候,会释放锁;在消费 meal 后,会将 meal 置为 null,操作期间需要获得 chef 的锁。

实现

代码语言:javascript
复制
public class Restaurant {
    Meal meal;
    ExecutorService exec = Executors.newCachedThreadPool();
    WaitPerson waitPerson = new WaitPerson(this);
    Chef chef = new Chef(this);
    
    public Restaurant() {
        exec.execute(chef);
        exec.execute(waitPerson);
    }
    
    public static void main(String[] args) {
        new Restaurant();
    }
}
代码语言:javascript
复制
public class Meal {
    
    private final int orderNum;
    
    public Meal(int orderNum) {
        this.orderNum = orderNum;
    }
    
    @Override
    public String toString() {
        return "Meal " + orderNum;
    }
}
代码语言:javascript
复制
public class Chef implements Runnable{
    private int count = 0;
    private Restaurant restaurant;
    
    public Chef(Restaurant restaurant) {
        this.restaurant = restaurant;
    }
    
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) { // shutdownNow 后,会退出循环
                synchronized (this) {
                    // 如果有 meal,则释放锁,等待
                    while (restaurant.meal != null) {
                        wait();
                    }
                }
                if (++ count == 10) {
                    System.out.println("Out of 10");
                    restaurant.exec.shutdownNow();
                }
                synchronized (restaurant.waitPerson) {
                    restaurant.meal = new Meal(count);
                    restaurant.waitPerson.notifyAll();
                }
                TimeUnit.SECONDS.sleep(1);
            }
            
        } catch (Exception e) {
            System.out.println("Chef interrupted");
        }
    }
}
代码语言:javascript
复制
public class WaitPerson implements Runnable{
    
    private Restaurant restaurant;
    
    public WaitPerson(Restaurant restaurant) {
        this.restaurant = restaurant;
    }
    
    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal == null) {
                        wait();
                    }
                }
                System.out.println("WaitPerson got " + restaurant.meal);
                synchronized (restaurant.chef) {
                    restaurant.meal = null;
                    restaurant.chef.notifyAll();
                }
            }
        } catch (Exception e) {
            System.out.println("WaitPerson interrupted");
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017.08.07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 思路
  • 实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档