线程的基本应用——生产者消费者模式

一旦牵涉到生产者(卖家)消费者(买家)

有一个生产者,一个消费者,生产者没有商品的时候进入生产时间,消费者需要等待。如果不需要生产,那么消费者可以直接购买。二对于消费者来说,在商品不需要生产的时候,可以直接买。在商品需要生产的时候,则需要等待。而生产者、消费者两者之间的纽带则是商品(商品是否已经生产)。

package com.test.week.six.monday;
class Goods{//商品类
    private String name;//商品名称
    private double price;//商品价格
    private boolean isshengchan;//是否再生产

    public Goods(String name, double price, boolean isshengchan) {
        this.name = name;
        this.price = price;
        this.isshengchan = isshengchan;
    }

    public String getName() {
        return name;
    }

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

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public boolean isIsshengchan() {
        return isshengchan;
    }

    public void setIsshengchan(boolean isshengchan) {
        this.isshengchan = isshengchan;
    }
}
class Client implements Runnable{//消费者线程
    private Goods goods;

    public Client(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
        while (true){
            synchronized (goods){
                if (!goods.isIsshengchan()){
                    //直接购买
                    System.out.println("消费者消费:"+goods.getName()+"   消费金额"+goods.getPrice()+"元");
                    //买完以后 再生产需求改写为true
                    goods.setIsshengchan(true);
//                    唤醒厂家干活
                    goods.notify();
                }else {
//                    不买  消费者等待
                    try {
                        goods.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
class Vender implements Runnable{//厂家线程
    private Goods goods;

    public Vender(Goods goods) {
        this.goods = goods;
    }

    @Override
    public void run() {
//        创建变量表示奇偶
        int count = 0;
        while (true){
//            生产者睡眠等待消费者消费
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (goods){
//                判断是否需要再生产
                if (goods.isIsshengchan()){
//                    判断奇偶,生产不同商品
                    if (count % 2 == 0){
                        goods.setName("泡面");
                        goods.setPrice(4.5);
                    }else {
                        goods.setName("香肠");
                        goods.setPrice(3);
                    }
                    count++;
//                    生产完成,唤醒消费者消费
                    goods.notify();
                    System.out.println("厂家生产了:"+goods.getName()+"   售价为:"+goods.getPrice()+"元");
                    goods.setIsshengchan(false);
                }else {
                    //不需要生产,线程等待
                    try {
                        goods.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }
        }
    }
}
public class Demo1 {
    public static void main(String[] args) {
        Goods goods = new Goods("大鸡腿", 6, false);
        Client client = new Client(goods);
        Vender vender = new Vender(goods);
        new Thread(client).start();
        new Thread(vender).start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值