您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 1浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Java并发多线程编程——通过生产者消费者模型理解等待唤醒机制

小志的博客 发布时间:2021-05-20 23:05:01 ,浏览量:1

一、通过生产者消费者模型理解(等待唤醒机制)

1、创建一个产品商城类

package com.xz.thread.t14;

/**
 * @description: 创建一个产品商城类
 * @author: xz
 * @create: 2021-05-20 21:59
 */
public class ProductStore {

    private int count;//产品数量
    public final int MAX_COUNT = 5;//产品数量最大值

    //生产产品的方法
    public synchronized void push(){
        while(count >= MAX_COUNT){
            try {
                System.out.println(Thread.currentThread().getName()+"库存数量已达到上限,生产者停止生产!");
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName()+"库存数量=【"+count+"】,生产者开始生产。。。。。");
        count++;
        notifyAll();//通知消费者消费
    }

    //消费产品的方法
    public synchronized void take(){
        while(count             
关注
打赏
1661269038
查看更多评论
0.0428s