Source of ThreadTester.java


  1: // Fig. 23.5: ThreadTester.java
  2: // Multiple threads printing at different intervals.
  3: import java.util.concurrent.Executors;
  4: import java.util.concurrent.ExecutorService;
  5: 
  6: public class ThreadTester 
  7: {
  8:    public static void main( String[] args )
  9:    {
 10:       // create and name each thread
 11:       PrintThread thread1 = new PrintThread( "thread1" );
 12:       PrintThread thread2 = new PrintThread( "thread2" );
 13:       PrintThread thread3 = new PrintThread( "thread3" );
 14:         
 15:       System.out.println( "Starting threads" );
 16: 
 17:       // create ExecutorService to manage threads
 18:       ExecutorService threadExecutor = Executors.newCachedThreadPool();
 19: 
 20:       // start threads and place in runnable state
 21:       threadExecutor.execute( thread1 ); // start thread1
 22:       threadExecutor.execute( thread2 ); // start thread2
 23:       threadExecutor.execute( thread3 ); // start thread3
 24: 
 25:       threadExecutor.shutdown(); // shutdown worker threads
 26:         
 27:       System.out.println( "Threads started, main ends\n" );
 28:    } // end main
 29: } // end class ThreadTester
 30: 
 31: 
 32: /**************************************************************************
 33:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 34:  * Pearson Education, Inc. All Rights Reserved.                           *
 35:  *                                                                        *
 36:  * DISCLAIMER: The authors and publisher of this book have used their     *
 37:  * best efforts in preparing the book. These efforts include the          *
 38:  * development, research, and testing of the theories and programs        *
 39:  * to determine their effectiveness. The authors and publisher make       *
 40:  * no warranty of any kind, expressed or implied, with regard to these    *
 41:  * programs or to the documentation contained in these books. The authors *
 42:  * and publisher shall not be liable in any event for incidental or       *
 43:  * consequential damages in connection with, or arising out of, the       *
 44:  * furnishing, performance, or use of these programs.                     *
 45:  *************************************************************************/