博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Java基础] java多线程关于消费者和生产者
阅读量:6171 次
发布时间:2019-06-21

本文共 4806 字,大约阅读时间需要 16 分钟。

多线程: 生产与消费 1.生产者Producer生产produce产品,并将产品放到库存inventory里;同时消费者Consumer从库存inventory里消费consume产品。 2.库存inventory的总量(最大库存量100)是有限的。如果库存inventory满了,生产者不能在库存inventory继续生产produce产品,须等待状态。等待产品被消费者Consumer消费consume了,再往库存inventory生产produce产品。 3.若库存inventory空了,消费者Consumer也须等待状态。等待生产者生产produce产品,再将消费者Consumer唤醒进行消费consume。 补充要求:需要6个线程,分别实现 1.库存为0;2.消费1消费10个(但库存零,则需等待);3.生产2生产5个;4.生产3生产5个(此时库存有10个,则满足消费1的消费;)最终库存仍有0个;5.生产4生产100个);6.生产5生产10个(因库存满了,无法生产,需等待)
import java.util.concurrent.BlockingQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.LinkedBlockingQueue; public class BlockingQueueTest { public static void main(String[] args) {  ExecutorService service = Executors.newCachedThreadPool();  BlockingQueue
blockingQueue = new LinkedBlockingQueue
(100); System.out.println("blockingQueue now contains " + blockingQueue.size() + " unit"); service.submit(new Consumer1(blockingQueue)); gap(blockingQueue); service.submit(new Productor2(blockingQueue)); gap(blockingQueue); service.submit(new Productor3(blockingQueue)); gap(blockingQueue); service.submit(new Productor4(blockingQueue)); gap(blockingQueue); service.submit(new Productor5(blockingQueue)); gap(blockingQueue); service.shutdown(); } private static void gap(BlockingQueue
blockingQueue) { try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("blockingQueue now contains " + blockingQueue.size() + " unit"); }}class Consumer1 implements Runnable{ BlockingQueue
blockingQueue; public Consumer1(BlockingQueue
blockingQueue) { super(); this.blockingQueue = blockingQueue; } @Override public void run() { // TODO Auto-generated method stub System.out.println("Consumer1 start: need 10 units"); for(int i = 0; i < 10; i++){ try { blockingQueue.take(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Consumer1 end: has got 10 units"); } }class Productor2 implements Runnable{ BlockingQueue
blockingQueue; public Productor2(BlockingQueue
blockingQueue) { super(); this.blockingQueue = blockingQueue; } @Override public void run() { // TODO Auto-generated method stub System.out.println("Productor2 start: put 5 units"); for(int i = 0; i < 5; i++){ try { blockingQueue.put("Object"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Productor2 end: has put 5 units"); } }class Productor3 implements Runnable{ BlockingQueue
blockingQueue; public Productor3(BlockingQueue
blockingQueue) { super(); this.blockingQueue = blockingQueue; } @Override public void run() { // TODO Auto-generated method stub System.out.println("Productor3 start: put 5 units"); for(int i = 0; i < 5; i++){ try { blockingQueue.put("Object"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Productor3 end: has put 5 units"); } }class Productor4 implements Runnable{ BlockingQueue
blockingQueue; public Productor4(BlockingQueue
blockingQueue) { super(); this.blockingQueue = blockingQueue; } @Override public void run() { // TODO Auto-generated method stub System.out.println("Productor4 start: put 100 units"); for(int i = 0; i < 100; i++){ try { blockingQueue.put("Object"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Productor4 end: has put 100 units"); } }class Productor5 implements Runnable{ BlockingQueue
blockingQueue; public Productor5(BlockingQueue
blockingQueue) { super(); this.blockingQueue = blockingQueue; } @Override public void run() { // TODO Auto-generated method stub System.out.println("Productor5 start: put 10 units"); for(int i = 0; i < 100; i++){ try { blockingQueue.put("Object"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("Productor5 end: has put 10 units"); } }

每个线程是隔了1s启动的, 结果

blockingQueue now contains 0 unit

Consumer1 start: need 10 units
blockingQueue now contains 0 unit
Productor2 start: put 5 units
Productor2 end: has put 5 units
blockingQueue now contains 0 unit
Productor3 start: put 5 units
Productor3 end: has put 5 units
Consumer1 end: has got 10 units
blockingQueue now contains 0 unit
Productor4 start: put 100 units
Productor4 end: has put 100 units
blockingQueue now contains 100 unit
Productor5 start: put 10 units
blockingQueue now contains 100 unit

 
分类: 
本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/4126164.html如需转载请自行联系原作者
demoblog
你可能感兴趣的文章
sorting, two pointers(cf div.3 1113)
查看>>
Scala并发编程【消息机制】
查看>>
win10下安装Oracle 11g 32位客户端遇到INS-13001环境不满足最低要求
查看>>
AngularJS-01.AngularJS,Module,Controller,scope
查看>>
【MySQL 安装过程1】顺利安装MySQL完整过程
查看>>
Inno Setup入门(二十)——Inno Setup类参考(6)
查看>>
图片自适应
查看>>
amd cmd
查看>>
Linux下的uml画图工具
查看>>
xml返回数组数据
查看>>
约瑟夫问题总结
查看>>
spring mybatis 批量插入返回主键
查看>>
指针函数小用
查看>>
开源力量公开课第二十三期-从SVN到Git,次时代代码管理
查看>>
输入挂
查看>>
升级迁移前,存储过程统计各个用户下表的数据量,和迁移后的比对
查看>>
sql注入分类
查看>>
初识CSS选择器版本4
查看>>
[Hadoop in China 2011] 朱会灿:探析腾讯Typhoon云计算平台
查看>>
JavaScript之数组学习
查看>>