An Enhanced Prototype of Producer-Consumer
In the context of multiple producers and consumers, the same times of producing and consuming occur alternately. For example, four times of push, then four times of pop, then four times of push again, etc. It is evident that the max length minuses the min length of the queue is value of the same times.
1 Producer.java
package thread; import java.util.Random; import entity.Queue; public class Producer extends Thread { private Queue queue; public Producer(int id, Queue queue) { this.queue = queue; this.setName("producer-" + id); } @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){} } } }
2 Consumer.java
package thread; import java.util.Random; import entity.Queue; public class Consumer extends Thread { private Queue queue; public Consumer(int id, Queue queue) { this.queue = queue; this.setName("consumer-" + id); } @Override public void run() { while (true) { queue.pop(); Random r = new Random(); try { Thread.sleep(r.nextInt(3)*1000); }catch(Exception e){} } } }
3 Queue.java
package entity; import java.util.ArrayList; import java.util.List; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class Queue { private int maxLen; private int minLen; private boolean pushOp = true; private List<Integer> list = new ArrayList<Integer>(); private Lock lock = new ReentrantLock(); private Condition proCondition = lock.newCondition(); private Condition conCondition = lock.newCondition(); public Queue(int maxLen, int minLen) { this.maxLen = maxLen; this.minLen = minLen; } public void push(Integer value) { try { lock.lock(); while (list.size() == maxLen) { proCondition.await(); } if (pushOp) { list.add(value); System.out.println(Thread.currentThread().getName() + " push=" + value + ", List=" + list.toString()); if (list.size() == maxLen) { pushOp = false; conCondition.signalAll(); } } } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } } public Integer pop() { Integer value = null; try { lock.lock(); while (list.size() == minLen) { conCondition.await(); } if (!pushOp) { value = list.get(0); list.remove(0); System.out.println(Thread.currentThread().getName() + " pop=" + value + ", List=" + list.toString()); if (list.size()==minLen) { pushOp = true; proCondition.signalAll(); } } } catch (InterruptedException e) { e.printStackTrace(); }finally { lock.unlock(); } return value; } }
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, 1); // set the max & min length of the queue for (int i=0;i<10;i++) { // Fewer threads, such as 3, is ok too. new Producer(i+1, myQueue).start(); new Consumer(i+1, myQueue).start(); } } }
5 snapshot of running
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/32498/viewspace-2641352/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- A Prototype of Producer-Consumer
- Object.prototype.__proto__, [[prototype]] 和 prototypeObject
- NEQR: novel enhanced quantum representation
- Enhanced Invertible Encoding for Learned Image CompressionEncoding
- Prototype/ConstructorStruct
- JavaScript prototype 原型JavaScript原型
- JavaScript:原型(prototype)JavaScript原型
- JS 系列二:深入 constructor、prototype、__proto__、[[Prototype]] 及 原型鏈JSStruct原型
- __proto__和prototype
- Function.prototype.callFunction
- JavaScript prototype原型用法JavaScript原型
- JavaScript prototype屬性JavaScript
- PDFsam Enhanced 7,專業PDF編輯軟體
- Javascript - prototype、__proto__、constructorJavaScriptStruct
- prototype實現繼承繼承
- 深究Function.prototype.bindFunction
- 建立模式 02-Prototype(原型)模式原型
- prototype 與 __proto__區別
- 簡話 prototype 和 __proto__
- 理解js的 prototype原型物件JS原型物件
- 強大的Array.prototype.splice()
- Array.prototype.reduce 實用指南
- 連結字串String.prototype.format字串ORM
- 設計模式-原型模式(Prototype)設計模式原型
- 解析Array.prototype.slice.call(argume
- OpenHarmony 3.1 Release版本關鍵特性解析——Enhanced SWAP記憶體管理記憶體
- Cryptanalyzing and Improving a Novel Color Image Encryption Algorithm Using RT-Enhanced Chaotic Tent MapsGo
- Node.js new 、 prototype 與 __proto__Node.js
- 設計模式--原型模式(Prototype Pattern)設計模式原型
- Function.prototype.call.apply作用詳解FunctionAPP
- JS 中的物件導向 prototype classJS物件
- Array.prototype.splice 的一個坑
- INFO1113 / COMP9003 a prototype of the gameGAM
- Why Object.prototype.hasOwnProperty.call(myObj, prop)Object
- 全面瞭解 Javascript Prototype Chain 原型鏈JavaScriptAI原型
- 帶你熟悉CCE叢集增強型CPU管理策略enhanced-static
- javascript中的prototype和__proto__的理解JavaScript
- Array.prototype.reduce 的理解與實現