生产消费者1
来源:互联网 发布:2016年药品中标数据网 编辑:程序博客网 时间:2024/05/04 23:10
package com.packtpub.java7.concurrency.chapter2.recipe2.core;import com.packtpub.java7.concurrency.chapter2.recipe2.task.Consumer;import com.packtpub.java7.concurrency.chapter2.recipe2.task.EventStorage;import com.packtpub.java7.concurrency.chapter2.recipe2.task.Producer;/** * Main class of the example */public class Main { /** * Main method of the example */ public static void main(String[] args) { // Creates an event storage EventStorage storage=new EventStorage(); // Creates a Producer and a Thread to run it Producer producer=new Producer(storage); Thread thread1=new Thread(producer); // Creates a Consumer and a Thread to run it Consumer consumer=new Consumer(storage); Thread thread2=new Thread(consumer); // Starts the thread thread2.start(); thread1.start(); }}
package com.packtpub.java7.concurrency.chapter2.recipe2.task;import java.util.Date;import java.util.LinkedList;import java.util.List;/** * This class implements an Event storage. Producers will storage * events in it and Consumers will process them. An event will * be a java.util.Date object * */public class EventStorage { /** * Maximum size of the storage */ private int maxSize; /** * Storage of events */ private List<Date> storage; /** * Constructor of the class. Initializes the attributes. */ public EventStorage(){ maxSize=10; storage=new LinkedList<>(); } /** * This method creates and storage an event. */ public synchronized void set(){ while (storage.size()==maxSize){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } storage.add(new Date()); System.out.printf("Set: %d\n",storage.size()); notify(); } /** * This method delete the first event of the storage. */ public synchronized void get(){ while (storage.size()==0){ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.printf("Get: %d: %s",storage.size(),((LinkedList<?>)storage).poll()); notify(); }}
package com.packtpub.java7.concurrency.chapter2.recipe2.task;/** * This class implements a producer of events. * */public class Producer implements Runnable { /** * Store to work with */ private EventStorage storage; /** * Constructor of the class. Initialize the storage. * @param storage The store to work with */ public Producer(EventStorage storage){ this.storage=storage; } /** * Core method of the producer. Generates 100 events. */ @Override public void run() { for (int i=0; i<100; i++){ storage.set(); } }}
package com.packtpub.java7.concurrency.chapter2.recipe2.task;/** * This class implements a consumer of events. * */public class Consumer implements Runnable { /** * Store to work with */ private EventStorage storage; /** * Constructor of the class. Initialize the storage * @param storage The store to work with */ public Consumer(EventStorage storage){ this.storage=storage; } /** * Core method for the consumer. Consume 100 events */ @Override public void run() { for (int i=0; i<100; i++){ storage.get(); } }}
结果:
Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:51 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016Set: 1
Set: 2
Set: 3
Set: 4
Set: 5
Set: 6
Set: 7
Set: 8
Set: 9
Set: 10
Get: 10: Tue Jul 12 16:05:52 CST 2016Get: 9: Tue Jul 12 16:05:52 CST 2016Get: 8: Tue Jul 12 16:05:52 CST 2016Get: 7: Tue Jul 12 16:05:52 CST 2016Get: 6: Tue Jul 12 16:05:52 CST 2016Get: 5: Tue Jul 12 16:05:52 CST 2016Get: 4: Tue Jul 12 16:05:52 CST 2016Get: 3: Tue Jul 12 16:05:52 CST 2016Get: 2: Tue Jul 12 16:05:52 CST 2016Get: 1: Tue Jul 12 16:05:52 CST 2016
以上代码来源于Java7并发实践手册
- 生产消费者1
- 生产消费者力量
- 生产消费者 多线程 代码
- 生产消费者模型
- 未完成-生产消费者模型
- 生产消费者问题代码
- 生产消费者模式
- linux生产消费者模型
- 生产消费者问题 CreateMutex
- 生产消费者模式
- 生产消费者模式
- 线程池-生产消费者
- 生产,消费者改进
- java 生产消费者问题
- 生产消费者模式
- 消费者生产模式
- 生产、消费者模式
- 生产、消费者模式
- tomcat搭建并配置
- windows server 2003启动多个tomcat服务环境变量配置
- 快速幂
- C++中Vector保存 原始指针和智能指针的析构问题
- excel操作
- 生产消费者1
- Android解决ADB not responding问题
- Java 读取properties文件
- 1ibatis配置
- 从应用到平台 – 云服务架构的演进过程
- SqlServer设置
- 2ibatis语义
- u3d第一发使用ugui完成消灭星星游戏(一)
- 常见校验算法