生产消费者1
2016-07-12 16:14
555 查看
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并发实践手册
相关文章推荐
- 生产消费者4 - 实现一个基于优先级的传输队列【消费顺序是由优先级决定的而不是抵达时间】
- Java多线程-生产消费者2
- Kafka下的生产消费者模式与订阅发布模式
- RabbitMQ下的生产消费者模式与订阅发布模式
- Java多线程Queue、BlockingQueue和使用BlockingQueue实现生产消费者模型方法解析
- excel操作
- UE4使用widget创建UI界面播放视频
- TCP和UDP?
- 不能从usb引导安装系统的解决方法
- 快速幂
- Linux-KVM源代码结构介绍
- vCenter6.0配置一:配置分布式交换机
- vCenter6.0配置一:配置分布式交换机
- 不利用svn server 新建用户,如何解决
- linux关闭tcp连接工具--killcx
- Java中的并发库学习总结
- HTTP和HTTPS的区别?
- swift 项目搭建
- (07)Hibernate的inverse属性和cascade属性
- Ext3.x FileUploadField 上传文件 方法