wait/notify实现生产消费者模式
来源:互联网 发布:js鼠标移上去图片放大 编辑:程序博客网 时间:2024/04/29 20:34
存储类GoodStorage:
import java.util.ArrayList;import java.util.List;public class GoodStorage {private static final int SIZE = 2;private List<Good> storage = new ArrayList<Good>(SIZE);public void add(Good good){synchronized (storage){while (storage.size() >= SIZE){try {System.out.println(Thread.currentThread().getName() + "------>product wait.... now ListSIze:" +storage.size());storage.wait();} catch (InterruptedException e) {e.printStackTrace();}}storage.add(good);System.out.println(Thread.currentThread().getName() + "------>product :"+ good.toString() + ", now ListSIze:" +storage.size());storage.notifyAll();}}public void remove(){synchronized (storage){while (storage.size() == 0){try {System.out.println(Thread.currentThread().getName() + "------>consume wait.... now ListSIze:" +storage.size());storage.wait();} catch (InterruptedException e) {e.printStackTrace();}}Good good = storage.get(0);storage.remove(0);System.out.println(Thread.currentThread().getName() + "------------------------------------>consume :"+ good.toString()+ ", now ListSIze:" +storage.size());storage.notifyAll();}}}class Good {public long id;public Good(long id) {this.id = id;}public String toString() {return String.format("Good-%d", this.id);}}
生产者:
import java.util.Random;import java.util.concurrent.atomic.AtomicInteger;class Producter implements Runnable {private static AtomicInteger ato = new AtomicInteger(0);private GoodStorage store;public Producter(GoodStorage store ){this.store = store;}public void run() {while(true){try {Thread.currentThread().sleep(new Random().nextInt(1000));// queue.put(new Good(System.currentTimeMillis()));store.add(new Good(ato.incrementAndGet()));} catch (InterruptedException e) {e.printStackTrace();}}}}
消费者:
import java.util.Random;class Consumer implements Runnable {private GoodStorage store;public Consumer(GoodStorage store ){this.store = store;}public void run() {while(true){try {Thread.currentThread().sleep(new Random().nextInt(1000));store.remove();} catch (InterruptedException e) {e.printStackTrace();}}}}
运行主类:
import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Main {static ExecutorService pool = Executors.newCachedThreadPool();public static void main(String[] args) {GoodStorage store = new GoodStorage();pool.execute(new Producter(store));pool.execute(new Producter(store));pool.execute(new Producter(store));pool.execute(new Consumer(store));pool.execute(new Consumer(store));}}
运行结果:
pool-1-thread-4------>consume wait.... now ListSIze:0pool-1-thread-1------>product :Good-1, now ListSIze:1pool-1-thread-4------------------------------------>consume :Good-1, now ListSIze:0pool-1-thread-3------>product :Good-2, now ListSIze:1pool-1-thread-1------>product :Good-3, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-2, now ListSIze:1pool-1-thread-1------>product :Good-5, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-3, now ListSIze:1pool-1-thread-1------>product :Good-7, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-5, now ListSIze:1pool-1-thread-3------>product :Good-4, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-7, now ListSIze:1pool-1-thread-1------>product :Good-9, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-4, now ListSIze:1pool-1-thread-2------>product :Good-6, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-9, now ListSIze:1pool-1-thread-3------>product :Good-8, now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-6, now ListSIze:1pool-1-thread-3------>product :Good-10, now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-8, now ListSIze:1pool-1-thread-2------>product :Good-11, now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-10, now ListSIze:1pool-1-thread-2------>product :Good-14, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-11, now ListSIze:1pool-1-thread-1------>product :Good-12, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-14, now ListSIze:1pool-1-thread-3------>product :Good-13, now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-12, now ListSIze:1pool-1-thread-2------>product :Good-15, now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-13, now ListSIze:1pool-1-thread-5------------------------------------>consume :Good-15, now ListSIze:0pool-1-thread-3------>product :Good-16, now ListSIze:1pool-1-thread-1------>product :Good-17, now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-16, now ListSIze:1pool-1-thread-2------>product :Good-18, now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-17, now ListSIze:1pool-1-thread-5------------------------------------>consume :Good-18, now ListSIze:0pool-1-thread-3------>product :Good-19, now ListSIze:1pool-1-thread-2------>product :Good-20, now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-19, now ListSIze:1pool-1-thread-3------>product :Good-21, now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-20, now ListSIze:1pool-1-thread-1------>product :Good-22, now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-21, now ListSIze:1pool-1-thread-4------------------------------------>consume :Good-22, now ListSIze:0pool-1-thread-2------>product :Good-23, now ListSIze:1pool-1-thread-3------>product :Good-24, now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-23, now ListSIze:1pool-1-thread-3------>product :Good-27, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-24, now ListSIze:1pool-1-thread-3------>product :Good-28, now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-27, now ListSIze:1pool-1-thread-3------>product :Good-29, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-28, now ListSIze:1pool-1-thread-1------>product :Good-25, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-29, now ListSIze:1pool-1-thread-2------>product :Good-26, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-25, now ListSIze:1pool-1-thread-1------>product :Good-31, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-26, now ListSIze:1pool-1-thread-2------>product :Good-32, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-31, now ListSIze:1pool-1-thread-1------>product :Good-33, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-32, now ListSIze:1pool-1-thread-3------>product :Good-30, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-33, now ListSIze:1pool-1-thread-3------>product :Good-35, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-30, now ListSIze:1pool-1-thread-1------>product :Good-36, now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-35, now ListSIze:1pool-1-thread-2------>product :Good-34, now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-36, now ListSIze:1pool-1-thread-1------>product :Good-37, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-34, now ListSIze:1pool-1-thread-2------>product :Good-40, now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-37, now ListSIze:1pool-1-thread-3------>product :Good-38, now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-4------------------------------------>consume :Good-40, now ListSIze:1pool-1-thread-2------>product :Good-41, now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2pool-1-thread-5------------------------------------>consume :Good-38, now ListSIze:1pool-1-thread-1------>product :Good-39, now ListSIze:2pool-1-thread-3------>product wait.... now ListSIze:2pool-1-thread-2------>product wait.... now ListSIze:2pool-1-thread-1------>product wait.... now ListSIze:2
0 0
- wait/notify实现生产消费者模式
- java wait()/notify() 实现生产者消费者模式
- 使用wait/notify实现生产者消费者模式
- 使用Object.wait与notify实现生产者与消费者模式
- Java synchronized、wait、notify实现线程(生成消费者模式)
- Thread详解9:用wait/notify实现生产者/消费者模式
- 用wait和notify/notifyAll实现生产者消费者模式
- wait/notify实现生产消费模型
- Wait, notify实现的生产消费模型
- wait/notify实现生产消费过程
- wait()、notify()实现生产者、消费者问题
- wait()+notify实现java生产者-消费者
- 使用wait/notify实现生产者消费者
- 生产者与消费者之实现(wait、notify)
- Java多线程通信-利用传统的线程通信wait(),notify()方法实现“生产者消费者模式”
- 使用Object类中的wait()和notify()方法实现生产者和消费者模式
- Java中通过wait和notify来实现生产者和消费者模式
- Java多线程——使用wait/notify实现生产者/消费者模式
- 破解时常用的汇编指令
- http请求部分常见的状态码
- easyui 窗口的onBeforeClose 使用
- 字符串哈希函数
- 教你如何绘制圆形头像
- wait/notify实现生产消费者模式
- android radiobutton 设置选中问题
- 自动构建工具Grunt
- android Process 18869 exceeded cursor quota 100, will kill it
- Roman to Integer
- RequiredFieldValidator控件验证不能为空时报错多种解决方法以及问题分析
- 关于tr、td(1)
- Android取消EditText自动获取焦点默认行为
- 权限拦截器