java中生产者与消费者问题

package com.okme.mythread;


/**
 * 两个线程协同工作,先生产,后消费
 */
public class ProducterCustomerdemo {
    public static void main(String[] args) {

        Food food = new Food();
        Producter p = new Producter(food);
        Customers c = new Customers(food);

        Thread b1 = new Thread(p);
        Thread b2 = new Thread(c);

        b1.start();
        b2.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;//ture表示可以生产,false表示可以消费

    /**
     * 谁有属性,谁提供方法
     * @return
     */

    //生产产品
    public synchronized void set(String name, String desc){
        if (!flag){
            try {
                this.wait();//线程进入等待状态,释放监视器的所有权(对象锁)
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.setName(name);
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        this.setDesc(desc);
        flag = false;
        this.notify();//唤醒等待的随机一个线程,notifyall唤醒等待的全部线程
    }

    //消费产品
    public synchronized void get(){
        if (flag){
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(this.getName()+"->"+this.desc);
        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() {
    }
}
 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值