Source of SharedBufferTest2.java


  1: // Fig 23.20: SharedBufferTest2.java
  2: // Application shows two threads manipulating a synchronized buffer.
  3: import java.util.concurrent.ExecutorService;
  4: import java.util.concurrent.Executors;
  5: 
  6: public class SharedBufferTest2
  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 SynchronizedBuffer to store ints
 14:       Buffer sharedLocation = new SynchronizedBuffer();
 15: 
 16:       System.out.printf( "%-40s%s\t\t%s\n%-40s%s\n\n", "Operation", 
 17:          "Buffer", "Occupied", "---------", "------\t\t--------" );
 18: 
 19:       try // try to start producer and consumer
 20:       {
 21:          application.execute( new Producer( sharedLocation ) );
 22:          application.execute( new Consumer( sharedLocation ) );
 23:       } // end try
 24:       catch ( Exception exception )
 25:       {
 26:          exception.printStackTrace();
 27:       } // end catch
 28: 
 29:       application.shutdown();
 30:    } // end main
 31: } // end class SharedBufferTest2
 32: 
 33: 
 34: 
 35: /**************************************************************************
 36:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 37:  * Pearson Education, Inc. All Rights Reserved.                           *
 38:  *                                                                        *
 39:  * DISCLAIMER: The authors and publisher of this book have used their     *
 40:  * best efforts in preparing the book. These efforts include the          *
 41:  * development, research, and testing of the theories and programs        *
 42:  * to determine their effectiveness. The authors and publisher make       *
 43:  * no warranty of any kind, expressed or implied, with regard to these    *
 44:  * programs or to the documentation contained in these books. The authors *
 45:  * and publisher shall not be liable in any event for incidental or       *
 46:  * consequential damages in connection with, or arising out of, the       *
 47:  * furnishing, performance, or use of these programs.                     *
 48:  *************************************************************************/