使用BlockingQueue实现生产者和消费者模式
来源:互联网 发布:1394mac 编辑:程序博客网 时间:2024/04/30 06:00
package com.hjx.product_customer;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;/** * 运行测试类 * @author hjx * */public class MainTest { public static void main(String[] args) { //存储所有线程的Map Map<String,Runnable> runnabelMap =new HashMap<String,Runnable>(); //仓库类 Storage storage =new Storage(); //这里使用带缓存功能的线程池 ExecutorService sercice = Executors.newCachedThreadPool(); Productor p1 = null; Customer c1 = null; String name =""; //循环创建各100个生产者线程和消费者线程 for (int i = 0; i < 100; i++) { name =i +"-p"; p1 = new Productor(true,storage,name); runnabelMap.put(name, p1); name =i + "-m"; c1 = new Customer(true,storage,name); runnabelMap.put(name, c1); sercice.submit(p1); sercice.submit(c1);}try {//休眠10秒后TimeUnit.SECONDS.sleep(10); //开始停止所有的线程Set set = runnabelMap.keySet();Iterator it = set.iterator();while (it.hasNext()) {String keyName = (String ) it.next();BaseRunnable r = (BaseRunnable) runnabelMap.get(keyName);r.setGoing(false); //设置线程停止的标识}//休眠10秒后TimeUnit.SECONDS.sleep(10);//查看仓库里还有多少产品未消费。int count = storage.getQueue().size();System.out.println("仓库里未消费的产品数量: " + count);} catch (InterruptedException e) {e.printStackTrace();} System.out.println("main is end!"); }}package com.hjx.product_customer;import java.util.concurrent.TimeUnit;/** * 生产者 * @author hjx * */public class Productor extends BaseRunnable {private Storage storage;private String name;public Productor(boolean isGoing,Storage storage,String name) {this.setGoing(isGoing);this.storage = storage;this.name = name;}@Overridepublic void run() {while (this.isGoing()) {String pName ="产品" + (int)(Math.random() * 10000);ProductInfo productInfo =new ProductInfo(pName);try {storage.push(productInfo);System.out.println(name +" 生产产品: "+ productInfo.toString());TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("生产者已经停止生产!");}}package com.hjx.product_customer;import java.util.concurrent.TimeUnit;/** * 消费者 * @author hjx * */public class Customer extends BaseRunnable {private Storage storage;private String name;public Customer(boolean isGoing,Storage storage,String name) {this.setGoing(isGoing);this.name = name;this.storage = storage;}@Overridepublic void run() {while (this.isGoing()) {try {ProductInfo p = storage.pop();System.out.println(name +" 消费产品: " +p.toString());TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}}System.out.println("消费者已经停止消费!");}}package com.hjx.product_customer;/** * 产品对象 * @author hjx * */public class ProductInfo {private String name;public ProductInfo(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "ProductInfo [name=" + name + "]";}}package com.hjx.product_customer;/** * 生产者、消费者的父类 * @author hjx * */public class BaseRunnable implements Runnable {private boolean isGoing;//生产者、消费者是否需要继续生产或者消费的条件public boolean isGoing() {return isGoing;}public void setGoing(boolean isGoing) {this.isGoing = isGoing;}@Overridepublic void run() {System.out.println("父类方法");}}
运行效果:
阅读全文
0 0
- 使用BlockingQueue实现生产者和消费者模式
- 使用BlockingQueue实现生产者消费者模式
- Java使用BlockingQueue实现生产者消费者模式
- 使用BlockingQueue实现生产者消费者模式
- 使用BlockingQueue实现生产者消费者模式
- 使用BlockingQueue实现生产者,消费者模式
- 使用BlockingQueue实现生产者和消费者模型
- BlockingQueue实现生产者、消费者模式
- BlockingQueue实现生产者消费者模式
- Java多线程-工具篇-BlockingQueue(实现生产者和消费者模式)
- Java利用BlockingQueue实现生产者和消费者模式
- 生产者和消费者问题-使用BlockingQueue来实现
- 基于BlockingQueue的生产者消费者模式实现
- 使用BlockingQueue创建生产者消费者模式
- BlockingQueue阻塞队列和生产者-消费者模式
- 使用Java的BlockingQueue实现生产者-消费者
- 使用Java的BlockingQueue实现生产者-消费者
- 使用Java的BlockingQueue实现生产者-消费者
- 葡萄城荣膺”2017中国智慧服务体验十强品牌”
- Eclipse上配置SVN
- Linux 软件包介绍---->RPM包管理
- Pandas学习笔记一(Pandas数据结构)
- kafka入门:简介、使用场景、设计原理、主要配置及集群搭建
- 使用BlockingQueue实现生产者和消费者模式
- 设计模式简介
- 基于Cocos2d-x学习OpenGL ES 2.0系列——编写自己的shader(2)
- 使用jQuery重置(reset)表单的方法
- Single Variable Calculus 总结
- Linux--计算机端口分类与定时器的分类
- 前后分离
- Linux常用命令整理
- 实时获取文本域中的长度,超出长度自动截取