当前位置:网站首页>生产者消费代码
生产者消费代码
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
边栏推荐
- 驱动领域DDD的微服务设计和开发实战
- I/O Code Practice
- 【GoLang】Sync lock
- Detailed evaluation of Renesas RZ/G2L processor
- 【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
- linux 安装mysql8.0 超详细教程(实战多次)
- 一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
- MQTT over QUIC:下一代物联网标准协议为消息传输场景注入新动力
- 小米法务部:成功打击一处伪造Redmi蓝牙耳机窝点,查扣假冒伪劣产品2032个
- error #6633: The type of the actual argument differs from the type of the dummy argument.
猜你喜欢
随机推荐
[Designers must learn] Lighting skills of Enscape in SketchUp
如果不知道这4种缓存模式,敢说懂缓存吗?
人脸关键点预测以及归一化
UFLO:5、启动任务并自动完成第一个人工任务
Alibaba 开源内网高并发编程手册
KDD'22博士论文奖:清华裘捷中成亚洲高校首位获得者,斯坦福Rex Ying获WINNER奖
不会多线程还想进BAT?精选19道多线程面试题,有答案边看边学
奇怪,为什么ArrayList初始化容量大小为10?
I/O代码实践
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
Contribution and writing required documents - OpenHarmony developer documentation style guide
打卡广汽本田喜悦安全驾驶中心,体验最刁钻的场地训练
一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
又一位AI大佬辞职进体制内!AI的尽头是编制?
我把小程序搬到了自有App中,开发效率提升了5倍
nacos实现基本的服务跨进程调用和使用OpenFeign进行服务跨进程调用
package-lock.json 有什么作用,如果项目中没有它会怎么样,举例说明
Mobileye展示与Intel合作开发的硅光子激光雷达芯片
金九银十必备 快来看看你还缺啥
See you in shenzhen!Cloud native to accelerate the application building special: see cloud native FinOps, SRE, high-performance computing scenario best practices