Question: Reimplement the following code using Lock and Condition concepts: 1 // Fig. 23.18: CircularBuffer.java 2 // Synchronizing access to a shared three-element bounded buffer. 3
Reimplement the following code using Lock and Condition concepts:
1 // Fig. 23.18: CircularBuffer.java 2 // Synchronizing access to a shared three-element bounded buffer. 3 public class CircularBuffer implements Buffer 4 { 5 private final int[] buffer = {-1, -1, -1}; // shared buffer 6 7 private int occupiedCells = 0; // count number of buffers used 8 private int writeIndex = 0; // index of next element to write to 9 private int readIndex = 0; // index of next element to read 10 11 // place value into buffer 12 public synchronized void blockingPut(int value) 13 throws InterruptedException 14 { 15 // wait until buffer has space available, then write value; 16 // while no empty locations, place thread in blocked state 17 while (occupiedCells == buffer.length) 18 { 19 System.out.printf("Buffer is full. Producer waits.%n"); 20 wait(); // wait until a buffer cell is free 21 } // end while 22 23 buffer[writeIndex] = value; // set new buffer value 24 25 // update circular write index 26 writeIndex = (writeIndex + 1) % buffer.length; 27 28 ++occupiedCells; // one more buffer cell is full 29 displayState("Producer writes " + value); 30 notifyAll(); // notify threads waiting to read from buffer 31 } 32 33 // return value from buffer 34 public synchronized int blockingGet() throws InterruptedException 35 { 36 // wait until buffer has data, then read value; 37 // while no data to read, place thread in waiting state 38 while (occupiedCells == 0) 39 { 40 System.out.printf("Buffer is empty. Consumer waits.%n"); 41 wait(); // wait until a buffer cell is filled 42 } // end while 43 44 int readValue = buffer[readIndex]; // read value from buffer 45 46 // update circular read index 47 readIndex = (readIndex + 1) % buffer.length; 48 49 --occupiedCells; // one fewer buffer cells are occupied 50 displayState("Consumer reads " + readValue); 51 notifyAll(); // notify threads waiting to write to buffer 52 53 return readValue; 54 } 55 56 // display current operation and buffer state 57 public synchronized void displayState(String operation) 58 { 59 // output operation and number of occupied buffer cells 60 System.out.printf("%s%s%d)%n%s", operation, 61 " (buffer cells occupied: ", occupiedCells, "buffer cells: "); 62 63 for (int value : buffer) 64 System.out.printf(" %2d ", value); // output values in buffer 65 66 System.out.printf("%n "); 67 68 for (int i = 0; i < buffer.length; i++) 69 System.out.print("---- "); 70 71 System.out.printf("%n "); 72 73 for (int i = 0; i < buffer.length; i++) 74 { 75 if (i == writeIndex && i == readIndex) 76 System.out.print(" WR"); // both write and read index 77 else if (i == writeIndex) 78 System.out.print(" W "); // just write index 79 else if (i == readIndex) 80 System.out.print(" R "); // just read index 81 else 82 System.out.print(" "); // neither index 83 } 84 85 System.out.printf("%n%n"); 86 } 87 } // end class CircularBuffer
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
