侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

【Java多线程二】多路条件解决生产者消费者问题

Java  /  管理员 发布于 3年前   169

package com.tom;import java.util.LinkedList;import java.util.Queue;import java.util.concurrent.ThreadLocalRandom;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;class Plate<T> {    private final Lock lock = new ReentrantLock();    private final Condition PLATE_NOT_FULL = lock.newCondition();    private final Condition PLATE_NOT_EMPTY = lock.newCondition();    private final Queue<T> plates = new LinkedList<T>();    private int size;    public Plate(int size) {        this.size = size;    }    public void produce(T fruit) {        lock.lock();        try {            if (plates.size() >= size) {                try {                    System.out.println(Thread.currentThread().getName() + ", The plate is full..waiting to put into the plate");                    PLATE_NOT_FULL.await();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            System.out.println(Thread.currentThread().getName() + ", The plate is not full..put the fruit  into the plate: " + fruit);            plates.offer(fruit);            PLATE_NOT_EMPTY.signalAll();        } finally {            if (lock != null){                lock.unlock();            }        }    }    public T consume() {        T fruit = null;        lock.lock();        try {            if (plates.isEmpty()) {                try {                    System.out.println(Thread.currentThread().getName() + ", The plate is empty..waiting to consume");                    PLATE_NOT_EMPTY.await();                } catch (InterruptedException e) {                    e.printStackTrace();                }            }            fruit = plates.poll();            System.out.println(Thread.currentThread().getName() + ",The plate is not empty, consume the fruit: " + fruit);            PLATE_NOT_FULL.signalAll();        } finally {            if (lock != null){                lock.unlock();            }        }        return fruit;    }}public class ThreadCommunication {    private static volatile boolean shutdown = false;    public static void main(String[] args) {        final Plate<Integer> plate = new Plate<Integer>(8);        Thread[] consumers = new Thread[3];        for (int i = 0; i < consumers.length; i++) {            consumers[i] = new Thread() {                @Override                public void run() {                    while (!shutdown) {                        plate.consume();                        try {                            Thread.sleep(200);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }            };            consumers[i].start();        }        Thread[] producers = new Thread[2];        for (int i = 0; i < producers.length; i++) {            producers[i] = new Thread() {                @Override                public void run() {                    while (!shutdown) {                        plate.produce(ThreadLocalRandom.current().nextInt(100, 1000));                        try {                            Thread.sleep(100);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                    }                }            };            producers[i].start();        }        try {            Thread.sleep(6000);            shutdown = true;        } catch (InterruptedException e) {            e.printStackTrace();        }    }}

 


  • 上一条:
    【Java】Java执行远程机器上Linux命令
    下一条:
    ThinkPHP通过AJAX返回JSON的两种实现方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • java正则表达式彻底研究(0个评论)
    • java正则表达式验证函数(0个评论)
    • MVC、MVP和MVVM分别是什么(0个评论)
    • java 单例模式(饿汉模式与懒汉模式)(0个评论)
    • 近期文章
    • ChatGPT再出新功能,推出插件功能,能联网、搜索了(0个评论)
    • 在go语言中使用GoPDF包把html生成PDF文件示例(0个评论)
    • 在go语言中创建和解析(读取)符号链接示例(0个评论)
    • ubuntu 22.04系统中报错:Python 3.6 is no longer supported by the Python core team...解决方式(0个评论)
    • Laravel 10.4版本发布(0个评论)
    • mysql5.7中实现分区表及分区where in查询示例及分区分表对比浅析(0个评论)
    • nginx + vue配置实现同域名下不同路径访问不同项目(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-11
    • 2018-03
    • 2020-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客