Thursday 15 March 2012

multithreading - Creating a Synchronized Buffer for Producer/Consumer pattern in Java -


I have applied a buffer for productive / consumer pattern, although it seems that consumers do not get lock, so Hunger is faster. I do not know why this happens because both put () and () think that the lock is to be released properly ...

I know that blocking queue and other good implementations , But I want to use it to wait () and inform as a practice.

  Public Category Maker Consumer Rouge {Public Stable Zero Main (String [] Args) {IntBuffer buffer = New Interfuffer (8); Consumer raw consumer = new consumer router (buffer); Manufacturer Row Manufacturer = New Manufacturer Rouge (Buffer); Thread T1 = New Thread (Consumer); Thread T2 = new thread (creator); T1.start (); T2.start (); }} Class ConsumerRaw Runnables {Private Final applies IntBuffer buffer; Public Consumer Rights (Intrabfer B) {Buffer = B; } Public Zero Run () (while! Buffer (iSett)) {int i = buffer.take (); System.out.println ("Consumer reads" + i); // This print can not be in order}}} Class creator Rara Runnabal {Private Final Interfer Buffer; Producer Rau (Intbuf B) {this.buffer = b; } Run for public null () (for (int i = 0; i & lt; 20; i ++) {int n = (int) (monte random () * 100); buffer.put (n); System .out.println ("manufacturer puts" + n);}}} class intbuffer {private fin int [] storage; personal volatile end-end; personal volatile int start; public integer (integer size) {this.storage = new int [Size]; end = 0; start = 0;} adds public zero (int n) {// put synchronized (storage) {boolean full = (start == (end + storage.lumb + 1)% storage. Length); while (full) {// UI is the best effort {storage.notifyAll (); storage.wait ();} Grip (interrupted ejection) e.printStackTrace ();}} This.storage [end] = n; end = increment increment (end); Storage.notifyAll ();}} Public int () {syncing (storage) {while (end == start) {// try blank quote {storage.notifyAll (); // notify waiters storage.wait ();} Grip (Interrupted e) e.printStackTrace ();}} int index = start; start = increment (start); storage.notifyAll (); // Notification Waiters return this return. Storage [index]; }} Private Inch Inchmandmode (Intex) {synchronize (storage) {if (index == storage.leng -1) returns 0; Other index 1; }} Public Boolean is empty () {syncing (collection) {return (start == end); } << Code>   

This is at least one problem, / code> method:

  boolean full = (start == (end + storage.lumb + 1)% storage.length);    complete  is never started as  true , then    Code>, how do you expect to end the loop? The  Other  problem in the consumer is the loop:  
  while (buffer. ISpeplay ()) {int i = buffer. (); System.out.println ("Consumer reads" + i); }   

You believe that the manufacturer never lets buffer empty - if the consumer starts before the producer, he will stop immediately.

Instead, you want something to say the buffer is that you have stopped production, the consumer should keep the queue empty until and will not get any more data .

No comments:

Post a Comment