当前位置:网站首页>生产者消费代码
生产者消费代码
2022-07-29 15:28:00 【[email protected]】
public class Storage {
private final int MAX_SIZE = 10;
private LinkedList<Integer> list = new LinkedList<>();
private final Lock lock = new ReentrantLock();
private final Condition full = lock.newCondition();
private final Condition empty = lock.newCondition();
private static int count = 1;
public void produce(){
try {
lock.lock();
while (list.size() + 1 > MAX_SIZE){
System.out.println("生产者[" + Thread.currentThread().getName() + "]仓库已满");
full.await();
}
list.add(count);
System.out.println("生产者[" + Thread.currentThread().getName() + "]生产了一个产品" + count++ + " , 库存量为" + list.size());
empty.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void consume() {
try {
lock.lock();
while (list.size() == 0){
System.out.println("消费者[" + Thread.currentThread().getName() + "]仓库为空");
empty.await();
}
int x = list.removeFirst();
System.out.println("消费者[" + Thread.currentThread().getName() + "]消费一个产品" + x +" , 库存量 " + list.size());
full.signal();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public class Consumer implements Runnable{
private Storage storage;
public Consumer() {
}
public Consumer(Storage storage) {
this.storage = storage;
}
@Override
public void run() {
while(true){
try{
Thread.sleep(3000);
storage.consume();
}catch (InterruptedException e){
e.printStackTrace();
}
}
}
}
public class Producer implements Runnable{
private Storage storage;
public Producer() {
}
public Producer(Storage storage) {
this.storage = storage;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000);
storage.produce();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class Main {
public static void main(String[] args) {
Storage storage = new Storage();
Thread p1 = new Thread(new Producer(storage),"生产者线程1");
Thread p2 = new Thread(new Producer(storage),"生产者线程2");
Thread p3 = new Thread(new Producer(storage),"生产者线程3");
Thread c1 = new Thread(new Consumer(storage),"消费者线程1");
Thread c2 = new Thread(new Consumer(storage),"消费者线程2");
Thread c3 = new Thread(new Consumer(storage),"消费者线程3");
p1.start();
p2.start();
p3.start();
c1.start();
c2.start();
c3.start();
}
}

。。。。
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_59287292/article/details/126023942
边栏推荐
猜你喜欢
![[yolov7 series two] positive and negative sample allocation strategy](/img/8d/5f04d33b957f3ce58243ea807c1fe2.png)
[yolov7 series two] positive and negative sample allocation strategy

易基因:人类tRNA基因位点表现出与衰老相关的DNA高甲基化|研究文章

Unable to open the source file in qt vs2015 "QtWidgets" solution

这个保护数据隐私的赛道,人人都想插一脚,互联网大厂挤破头,连甲方都下场自研了...

你真的了解Redis的持久化机制吗?

ES6 from entry to master # 11: the Map data type

教程篇(7.0) 07. 诊断和故障排除 * FortiClient EMS * Fortinet 网络安全专家 NSE 5

VMware 16.1软件安装包下载及安装教程

Compose要是不 `remember`,相关功能就实现不了了吗?

Alibaba 开源内网高并发编程手册
随机推荐
22届大专应届软件的同学你找到工作了吗
药品研发--质理研究人员绩效办法
开源数据库连接池的使用及其工具类
File management: logical structure
BGP联邦实验
CAN报文:数据帧详解
又一位AI大佬辞职进体制内!AI的尽头是编制?
Qt实战 | 如何获取USB设备信息?
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices
RestTemplate下载文件的另一种方式
SQL 开始日期、结束日期查询
化妆品行业分销渠道管理系统加强企业渠道管控能力,赋能化妆品渠道数字化升级
一文看懂分布式存储架构
联想集团宣布在科创板上市!杨元庆:进一步释放联想的价值!
如何创建NFT(还在创作中ing)
zabbix邮件发送告警信息
文件管理:文件的逻辑结构
DevOps的未来趋势
ES6 from entry to master # 11: the Map data type
Floating point memory storage problem