Source of SharedBufferTest.java


  1: // Fig 23.10: SharedBufferTest.java
  2: // Application shows two threads manipulating an unsynchronized buffer.
  3: import java.util.concurrent.ExecutorService;
  4: import java.util.concurrent.Executors;
  5: 
  6: public class SharedBufferTest
  7: {
  8:    public static void main( String[] args )
  9:    {
 10:       // create new thread pool with two threads
 11:       ExecutorService application = Executors.newFixedThreadPool( 2 );
 12: 
 13:       // create UnsynchronizedBuffer to store ints
 14:       Buffer sharedLocation = new UnsynchronizedBuffer();
 15: 
 16:       System.out.println( "Action\t\tValue\tProduced\tConsumed" );
 17:       System.out.println( "------\t\t-----\t--------\t--------\n" );
 18: 
 19:       // try to start producer and consumer giving each of them access
 20:       // to sharedLocation
 21:       try 
 22:       {
 23:          application.execute( new Producer( sharedLocation ) );
 24:          application.execute( new Consumer( sharedLocation ) );
 25:       } // end try
 26:       catch ( Exception exception )
 27:       {
 28:          exception.printStackTrace();
 29:       } // end catch
 30: 
 31:       application.shutdown(); // terminate application when threads end
 32:    } // end main
 33: } // end class SharedBufferTest
 34: 
 35: 
 36: 
 37: /**************************************************************************
 38:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 39:  * Pearson Education, Inc. All Rights Reserved.                           *
 40:  *                                                                        *
 41:  * DISCLAIMER: The authors and publisher of this book have used their     *
 42:  * best efforts in preparing the book. These efforts include the          *
 43:  * development, research, and testing of the theories and programs        *
 44:  * to determine their effectiveness. The authors and publisher make       *
 45:  * no warranty of any kind, expressed or implied, with regard to these    *
 46:  * programs or to the documentation contained in these books. The authors *
 47:  * and publisher shall not be liable in any event for incidental or       *
 48:  * consequential damages in connection with, or arising out of, the       *
 49:  * furnishing, performance, or use of these programs.                     *
 50:  *************************************************************************/