Java基础05 生产者与消费者案例 157 158

生产者与消费者应用案例

多线程的开发中有一个最经典的操作案例,就是生产者-消费者,生产者不断生产产品,消费者不断取走产品。
例如:饭店里的有一个厨师和一个服务员,这个服务员必须等待厨师准备好膳食。当厨师准备好时,他会通知服务员,之后服务员上菜,然后返回继续等待。这是一个任务协作的示例,厨师代表生产者,而服务员代表消费者。


  • 两个线程协同工作,先生产,再消费
  • 面试题 :
  • sleep 与 wait的区别?
  • sleep:让线程进入休眠状态,让出CPU的时间片,不释放对象监视器的所有权(对象锁)
  • wait:让线程进入等待状态,让出CPU的时间片,并释放对象监视器的所有权,等待其它线程通过notify方法来唤醒
    在这里插入图片描述
    注意wait是在synchronized即同步当中使用的
    notify唤醒的是随机的等待的线程
    notifyAll是唤醒所有等待线程

package com.vince;

/**
 * Created by vince on 2017/6/5.
 * 两个线程协同工作,先生产,再消费
 * 面试题 :
 * sleep 与 wait的区别?
 * sleep:让线程进入休眠状态,让出CPU的时间片,不释放对象监视器的所有权(对象锁)
 * wait:让线程进入等待状态,让出CPU的时间片,并释放对象监视器的所有权,等待其它线程通过notify方法来唤醒
 */
public class ProducterCustomerDemo {
    public static void main(String[] args) {
        Food food = new Food();
        Producter p = new Producter(food);
        Customers c = new Customers(food);
        Thread t1 = new Thread(p);
        Thread t2 = new Thread(c);
        t1.start();
        t2.start();
    }
}

/**
 * 消费者
 */
class Customers implements Runnable{
    private Food food;
    public Customers(Food food){
        this.food = food;
    }
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            food.get();
        }
    }
}

/**
 * 生产者
 */
class Producter implements Runnable{
    private Food food;

    public Producter(Food food){
        this.food = food;
    }
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            if(i%2==0){
                food.set("锅包肉","酸甜口味,爽");
            }else{
                food.set("佛跳墙","大补,滋阴补阳");
            }
        }
    }
}


/**
 * 食物
 */
class Food{
    private String name;
    private String desc;
    private boolean flag = true; //true表示可以生产,false表示可以消费
    /**
     * 生产产品
     */
    public synchronized void set(String name,String desc){
        //不能生产
        if(!flag){
            try {
                this.wait();//线程进入等待状态,释放监视器的所有权(对象锁)
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.setName(name);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.setDesc(desc);
        flag = false;
        this.notify();//唤醒等待的线程(随机的其中一个)
    }

    /**
     * 消费产品
     */
    public synchronized void get(){
        //不能消费
        if(flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(this.getName()+"->"+this.getDesc());
        flag = true;
        this.notify();
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "Food{" +
                "name='" + name + '\'' +
                ", desc='" + desc + '\'' +
                '}';
    }

    public Food(String name, String desc) {
        this.name = name;
        this.desc = desc;
    }

    public Food() {
    }
}

结果

在这里插入图片描述


学的比较浅
这里有个小点 记一下
在这里插入图片描述
要在一个类里 写另一个类的对象
就比如 要在Productor这个类 里面 写Food这个类的对象
那么怎么传进来这个对象呢
答案 使用构造方法
使用带参的构造方法就可以将Food这个类的对象传过来
同样的
传什么参数都可以 用构造方法即可往类里传递参数

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值