一个生产者-消费者模式实现方式

wait-notify模式的典型应用
它可以实现生产者-消费者模式

wait-notify模式的经典写法
生产者和消费者的逻辑都可以统一抽象成以下几个步骤:

step1:获得对象的锁;
step2:循环判断是否需要进行生产活动,如果不需要进行生产就调用wait方法,暂停当前线程;如果需要进行生产活动,进行对应的生产活动;
step3:通知等待线程

package com.zry.ProducerConsumer;

import static java.lang.Thread.sleep;

/**wait-notify模式的典型应用#
 * 它可以实现生产者-消费者模式
 *wait-notify模式的经典写法#
 * 生产者和消费者的逻辑都可以统一抽象成以下几个步骤:
 *
 * step1:获得对象的锁;
 * step2:循环判断是否需要进行生产活动,如果不需要进行生产就调用wait方法,暂停当前线程;如果需要进行生产活动,进行对应的生产活动;
 * step3:通知等待线程
 *
 * @author zry
 * @ClassName ProducerConsumer.java
 *
 * @createTime 2021年12月28日
 */

class AppleBox {
    private int appleCount;

    AppleBox(int appleCount) {
        this.appleCount = appleCount;
    }

    public synchronized void putInto() {
        while(appleCount >= 50) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String name = Thread.currentThread().getName();
        System.out.println("[" + name + "] 放入了一个苹果 , 箱子里有[" + ++appleCount + "]个苹果" );

        this.notifyAll();
    }

    public synchronized void takeAway() {

        while(appleCount <= 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String name = Thread.currentThread().getName();
        System.out.println("[" + name + "] 吃掉了一个苹果 , 箱子剩有[" + --appleCount + "]个苹果" );

        this.notifyAll();
    }

}
class Producer implements Runnable{
    private AppleBox appleBox;

    public Producer(AppleBox appleBox){
        this.appleBox=appleBox;
    }
    @Override
    public void run() {
        while(true){
            appleBox.putInto();
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

}

class Consumer implements Runnable{
    private AppleBox appleBox;

    public  Consumer( AppleBox appleBox){
        this.appleBox=appleBox;
    }
    @Override
    public void run() {
        while(true){
            appleBox.takeAway();
            try {
                sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
public class ProducerConsumer {
    public static void main(String[] args) {
        AppleBox box = new AppleBox(60);
        new Thread(new Producer(box),"Producer-1").start();
        new Thread(new Consumer(box),"Consumer-1").start();
        new Thread(new Consumer(box),"Consumer-2").start();
    }

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟猫喵喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值