A Prototype of Producer-Consumer
1 Queue.java
package entity; import java.util.ArrayList; import java.util.List; public class Queue { private int maxLen; private List<Integer> list = new ArrayList<Integer>(); public Queue(int maxLen) { this.maxLen = maxLen; } synchronized public void push(Integer value) { try { while (list.size() == maxLen) { this.wait(); } list.add(value); this.notifyAll(); System.out.println(Thread.currentThread().getName() + " push=" + value + ", List=" + list.toString()); } catch (InterruptedException e) { e.printStackTrace(); } } synchronized public Integer pop() { Integer value = null; try { while (list.size() == 0) { this.wait(); } value = list.get(0); list.remove(0); this.notifyAll(); System.out.println(Thread.currentThread().getName() + " pop=" + value + ", List=" + list.toString()); } catch (InterruptedException e) { e.printStackTrace(); } return value; } }
2 Producer.java
package thread; import java.util.Random; import entity.Queue; public class Producer extends Thread { private Queue queue; public Producer(Queue queue) { this.queue = queue; } @Override public void run() { while (true) { Random r = new Random(); int value = r.nextInt(10) + 100; queue.push(value); try { Thread.sleep(r.nextInt(3)*1000); }catch(Exception e){} } } }
3 Consumer.java
package thread; import java.util.Random; import entity.Queue; public class Consumer extends Thread { private Queue queue; public Consumer(Queue queue) { this.queue = queue; } @Override public void run() { while (true) { queue.pop(); Random r = new Random(); try { Thread.sleep(r.nextInt(3)*1000); }catch(Exception e){} } } }
4 Main.java
package app; import entity.Queue; import thread.Consumer; import thread.Producer; public class Main { public static void main(String[] args) throws InterruptedException{ Queue myQueue = new Queue(5); // set the max length of the queue for (int i=0;i<6;i++) { new Producer(myQueue).start(); new Consumer(myQueue).start(); } } }
5 snapshot of running
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/32498/viewspace-2641256/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- An Enhanced Prototype of Producer-Consumer
- Object.prototype.__proto__, [[prototype]] 和 prototypeObject
- JavaScript prototypeJavaScript
- Prototype/ConstructorStruct
- JavaScript prototype 原型JavaScript原型
- JavaScript:原型(prototype)JavaScript原型
- 關於prototype
- JavaScript prototype原型用法JavaScript原型
- Function.prototype.callFunction
- JavaScript prototype屬性JavaScript
- JavaScript中prototype用法JavaScript
- Array.prototype.indexOf()Index
- __proto__和prototype
- 原型模式(Prototype)原型模式
- 請教prototype模式!模式
- JS 系列二:深入 constructor、prototype、__proto__、[[Prototype]] 及 原型鏈JSStruct原型
- prototype實現繼承繼承
- 深究Function.prototype.bindFunction
- 原型模式(Prototype Pattern)。原型模式
- Array.prototype.slice.call
- 設計模式之Prototype設計模式
- Prototype與JQuery對比jQuery
- 孿生兄弟(Prototype) (轉)
- Javascript - prototype、__proto__、constructorJavaScriptStruct
- prototype 與 __proto__區別
- 建立模式 02-Prototype(原型)模式原型
- 簡話 prototype 和 __proto__
- Javascript篇之Prototype的原型JavaScript原型
- 理解js的 prototype原型物件JS原型物件
- javascript基礎(原型(prototype))(十七)JavaScript原型
- Prototype興衰啟示錄
- javascript prototype介紹的文章JavaScript
- 求教:模型設計之Prototype模型
- 強大的Array.prototype.splice()
- 解析Array.prototype.slice.call(argume
- Array.prototype.reduce 實用指南
- 談談 Object.prototype.toString 。Object
- JavaScript:prototype屬性使用方法JavaScript