Source of SynchronizedBuffer.java


  1: // Fig. 23.11: SynchronizedBuffer.java
  2: // SynchronizedBuffer synchronizes access to a single shared integer.
  3: import java.util.concurrent.locks.Lock;
  4: import java.util.concurrent.locks.ReentrantLock;
  5: import java.util.concurrent.locks.Condition;
  6: 
  7: public class SynchronizedBuffer implements Buffer
  8: {
  9:    // Lock to control synchronization with this buffer   
 10:    private Lock accessLock = new ReentrantLock();
 11: 
 12:    // conditions to control reading and writing
 13:    private Condition canWrite = accessLock.newCondition();
 14:    private Condition canRead = accessLock.newCondition();
 15: 
 16:    private int buffer = -1; // shared by producer and consumer threads
 17:    private boolean occupied = false; // whether buffer is occupied
 18:    
 19:    // place int value into buffer
 20:    public void set( int value )
 21:    {
 22:       accessLock.lock(); // lock this object
 23:                
 24:       // output thread information and buffer information, then wait
 25:       try
 26:       {
 27:          // while buffer is not empty, place thread in waiting state
 28:          while ( occupied ) 
 29:          {
 30:             System.out.println( "Producer tries to write." );
 31:             displayState( "Buffer full. Producer waits." );
 32:             canWrite.await();// wait until buffer is empty
 33:          } // end while
 34: 
 35:          buffer = value; // set new buffer value
 36: 
 37:          // indicate producer cannot store another value
 38:          // until consumer retrieves current buffer value
 39:          occupied = true;
 40:         
 41:          displayState( "Producer writes " + buffer );
 42: 
 43:          // signal thread waiting to read from buffer
 44:          canRead.signal(); 
 45:       } // end try
 46:       catch ( InterruptedException exception )
 47:       {
 48:          exception.printStackTrace();
 49:       } // end catch
 50:       finally
 51:       {
 52:          accessLock.unlock(); // unlock this object
 53:       } // end finally
 54:    } // end method set
 55:     
 56:    // return value from buffer
 57:    public int get()
 58:    {
 59:       int readValue = 0; // initialize value read from buffer
 60:       accessLock.lock(); // lock this object
 61: 
 62:       // output thread information and buffer information, then wait
 63:       try
 64:       {
 65:          // while no data to read, place thread in waiting state
 66:          while ( !occupied ) 
 67:          {
 68:             System.out.println( "Consumer tries to read." );
 69:             displayState( "Buffer empty. Consumer waits." );
 70:             canRead.await(); // wait until buffer is full
 71:          } // end while
 72: 
 73:          // indicate that producer can store another value 
 74:          // because consumer just retrieved buffer value
 75:          occupied = false;
 76: 
 77:          readValue = buffer; // retrieve value from buffer
 78:          displayState( "Consumer reads " + readValue );
 79: 
 80:          // signal thread waiting for buffer to be empty
 81:          canWrite.signal();
 82:       } // end try
 83:       // if waiting thread interrupted, print stack trace
 84:       catch ( InterruptedException exception ) 
 85:       {
 86:          exception.printStackTrace();
 87:       } // end catch
 88:       finally
 89:       {
 90:          accessLock.unlock(); // unlock this object
 91:       } // end finally
 92: 
 93:       return readValue;
 94:    } // end method get
 95:     
 96:    // display current operation and buffer state
 97:    public void displayState( String operation )
 98:    {
 99:       System.out.printf( "%-40s%d\t\t%b\n\n", operation, buffer, 
100:          occupied );
101:    } // end method displayState
102: } // end class SynchronizedBuffer
103: 
104: 
105: /**************************************************************************
106:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
107:  * Pearson Education, Inc. All Rights Reserved.                           *
108:  *                                                                        *
109:  * DISCLAIMER: The authors and publisher of this book have used their     *
110:  * best efforts in preparing the book. These efforts include the          *
111:  * development, research, and testing of the theories and programs        *
112:  * to determine their effectiveness. The authors and publisher make       *
113:  * no warranty of any kind, expressed or implied, with regard to these    *
114:  * programs or to the documentation contained in these books. The authors *
115:  * and publisher shall not be liable in any event for incidental or       *
116:  * consequential damages in connection with, or arising out of, the       *
117:  * furnishing, performance, or use of these programs.                     *
118:  *************************************************************************/