Java 线程与并发研究系列五(生产者与消费者)
来源:互联网 发布:手机打字软件 编辑:程序博客网 时间:2024/06/10 05:28
生产者与消费者问题是了解多线程的基本实例,就像hello world之于语言学习,下面我们就编写一个生产者-消费者的示例
public class Test {public static void main(String[] args) {Storage storage = new Storage(20);ExecutorService exes = Executors.newCachedThreadPool();for (int i = 0; i < 10; i++) {exes.submit(new Producer(storage));exes.submit(new Consumer(storage));}try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}exes.shutdownNow();}}class Producer implements Runnable{Storage storage;public Producer(Storage storage){this.storage = storage;}@Overridepublic void run() {try {storage.produce();} catch (InterruptedException e) {e.printStackTrace();}}}class Consumer implements Runnable{Storage storage;public Consumer(Storage storage){this.storage = storage;}@Overridepublic void run() {try {storage.consume();} catch (InterruptedException e) {e.printStackTrace();}}}class Storage{private static final int max = 30;private int num;public Storage(int num){this.num = num;}public synchronized void produce()throws InterruptedException{while(num>=max){wait();}num+=5;System.out.println("生产产品数:"+5+" 仓库还剩产品数:"+num);notifyAll();}public synchronized void consume()throws InterruptedException{while(num<7){wait();}num-=7;System.out.println("消费产品数:"+7+" 仓库还剩产品数:"+num);notifyAll();}}
以上是通过wait()和notifyAll synchronized关键字进行线程之间的同步,现在我们尝试另外一种方法,通过使用同步队列来解决任务的协作
问题,如果消费者试图从队列中获取对象,而此时队列为空,阻塞队列就会挂起消费者线程,同理如果参考已经满了,生产者试图向队列
中添加对象,此时队列将挂起生产者线程。
public class MyData{public static void main(String[] args) {Storage storage = new Storage();ExecutorService exes = Executors.newCachedThreadPool();for (int i = 0; i < 10; i++) {exes.submit(new Producer(storage));exes.submit(new Consumer(storage));}try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {e.printStackTrace();}exes.shutdownNow();System.out.println("仓库最后剩余数:"+storage.queue.size());}}class Producer implements Runnable{Storage storage;public Producer(Storage storage){this.storage = storage;}@Overridepublic void run() {try {storage.produce();} catch (InterruptedException e) {e.printStackTrace();}}}class Consumer implements Runnable{Storage storage;public Consumer(Storage storage){this.storage = storage;}@Overridepublic void run() {try {storage.consume();} catch (InterruptedException e) {e.printStackTrace();}}}class Storage{private static final int max = 30;public BlockingQueue<Product> queue;public Storage(){this.queue = new LinkedBlockingQueue<Product>();}public void produce()throws InterruptedException{if(queue.size()<max){queue.put(new Product());System.out.println("生产一个产品,仓库产品个数:"+queue.size());}}public void consume()throws InterruptedException{if(queue.size()>0){queue.take();System.out.println("消费一个产品,仓库产品个数:"+queue.size());}}}class Product{public Product(){}}
0 0
- Java 线程与并发研究系列五(生产者与消费者)
- Java多线程与并发(五)之生产者与消费者案例
- Java并发编程与技术内幕:消费者-生产者模式研究
- Java并发编程与技术内幕:消费者-生产者模式研究
- Java并发编程与技术内幕:消费者-生产者模式研究
- 经典问题生产者与消费者java并发线程模拟实现
- java线程 生产者与消费者
- java线程 生产者与消费者
- 线程生产者与消费者
- 线程---生产者与消费者
- java线程模拟生产者与消费者
- 生产者与消费者 java经典线程法
- JAVA线程(生产者与消费者)
- JAVA 线程间通信 - 生产者与消费者
- java线程经典之生产者与消费者
- java之多线程实例 生产者与消费者
- JAVA 线程间通信 - 生产者与消费者
- java线程通信 生产者与消费者模式
- SQL SERVER 合理的索引,会加快数据查询效率,但若索引过多或不当,则会适得其反
- 虚函数
- Codeforces 450 C. Jzzhu and Chocolate
- STL源码剖析 容器 stl_vector.h
- 今天心情很不好,PHP都看不懂代码了
- Java 线程与并发研究系列五(生产者与消费者)
- 我的博客
- UVA - 1416 Warfare And Logistics (最短路)
- 这个很好的
- CodeForce 356A Knight Tournament(set应用)
- seajs 2.3.0 添加jquery
- js原生offsetParent解析
- EM算法入门及简单应用举例
- 编程初学 封装的重要性