Java BlockingQueue生产者消费者实例
来源:互联网 发布:appsevr php升级 编辑:程序博客网 时间:2024/05/19 14:52
产品类:
/** * 产品 * * @author jiazhipeng * @version 1.0 * @date 2016-11-29 */public class Product { private int id; public Product(int id){ this.id = id; } public String toString(){ return "产品" + id; }}
仓库类:
/** * 仓库 * * @author jiazhipeng * @version 1.0 * @date 2016-11-29 */public class Storage { private BlockingQueue<Product> queue = new ArrayBlockingQueue<>(10); public synchronized void put(Product p) throws InterruptedException{ queue.put(p); } public Product take() throws InterruptedException{ return queue.take(); }}
生产者类:
/** * 生产者 * * @author jiazhipeng * @version 1.0 * @date 2016-11-29 */public class Producter implements Runnable{ // 自己的信号量 private Semaphore semo1; // 别人的信号量 private Semaphore semo2; private String name; private Storage storage; private Test test; public Producter(Storage storage, Test test, String name, Semaphore semo1, Semaphore semo2){ this.storage = storage; this.test = test; this.name = name; this.semo1 = semo1; this.semo2 = semo2; } @Override public void run() { for(; ; ){ try { semo1.acquire(); int i = test.getInt(); Product p = new Product(i); storage.put(p); System.out.println(name + "成功生产:" + p.toString()); semo2.release(); } catch (InterruptedException e) { e.printStackTrace(); break; } } }}
消费者类:
/** * 消费者 * * @author jiazhipeng * @version 1.0 * @date 2016-11-29 */public class Consumer implements Runnable{ private String name; private Storage storage; public Consumer(Storage storage, String name){ this.storage = storage; this.name = name; } @Override public void run() { for(; ; ){ try { Product p = storage.take(); System.out.println(name + "成功消费:" + p.toString()); } catch (Exception e) { e.printStackTrace(); break; } } }}
测试类:
/** * 测试 * * @author jiazhipeng * @version 1.0 * @date 2016-11-29 */public class Test { private int i = 1; public static void main(String[] args) { Test test = new Test(); Storage storage = new Storage(); Semaphore semo1 = new Semaphore(1); Semaphore semo2 = new Semaphore(0); Producter pr1 = new Producter(storage, test, "A1", semo1, semo2); Producter pr2 = new Producter(storage, test, "A2", semo2, semo1); Consumer cr1 = new Consumer(storage, "B1"); Consumer cr2 = new Consumer(storage, "B2"); ExecutorService service = Executors.newCachedThreadPool(); service.submit(pr1); service.submit(pr2); service.submit(cr1); service.submit(cr2); } public synchronized int getInt(){ return i++; }}
0 0
- Java BlockingQueue生产者消费者实例
- JAVA生产者与消费者模式 BlockingQueue与ExecutorService实例
- Java实现生产者消费者模型:多线程+BlockingQueue
- 使用Java的BlockingQueue实现生产者-消费者
- Java使用BlockingQueue实现生产者消费者模式
- 使用Java的BlockingQueue实现生产者-消费者
- 使用Java的BlockingQueue实现生产者-消费者
- 使用Java的BlockingQueue实现生产者-消费者
- 使用Java的BlockingQueue实现生产者-消费者
- Java Notes: BlockingQueue and 生产者消费者模式
- 使用Java的BlockingQueue实现生产者-消费者
- Java 生产者消费者实现——BlockingQueue
- 生产者-消费者-BlockingQueue
- BlockingQueue 生产者-消费者
- BlockingQueue生产者消费者
- java生产者消费者实例代码
- Java 生产者消费者并发实例
- java多线程-生产者消费者经典问题 基于BlockingQueue
- Android studio Error :...you can't have non-Gradle Java modules and Android-Gradle modules ...
- Mysql的数据查询语言DQL之基本查询
- win10系统下搭建mongodb服务
- 修改linux上ssh端口号
- 数据分析方法--PCA
- Java BlockingQueue生产者消费者实例
- LeetCode 61. Rotate List2.
- Mysql的数据查询语言DQL之连接查询
- 重新认识java(三) ---- 面向对象之继承!
- 视图,索引及储存过程的优缺点
- RocketMQ集群解决方案
- Spark系列修炼---入门笔记15
- Mysql的数据查询语言DQL之子查询
- Quartz定时任务