アクチュエータExecutor


1、newCachedThreadPool()プログラム実行中に必要数と同じスレッドを作成する2、newFixedThreadPool(int n)一度にn本の固定数のスレッドを予め作成する3、newSingleThreadExecutor()nが1のnewFixedThreadPoolに相当するスレッドを予め作成する
public class E1 implements Runnable {     
     private static int count = 0;
     private final int id = count++;     
     public E1() {
          System. out.println( "E1(" + id + ") start");
     }
     
     public void run() {
           int i = 3;
           while ( i-- > 0) {
              System. out.println( "E1(" + id + ") " + (i+1));
              Thread. yield();
          }
          System. out.println( "E1(" + id + ") stop");
     }


     public static void main(String[] args) {
//        ExecutorService executorService = Executors.newCachedThreadPool();
//        ExecutorService executorService = Executors.newFixedThreadPool(5);
          ExecutorService executorService = Executors.newSingleThreadExecutor();
           for( int i = 0; i < 5; i++) {
               executorService.execute( new E1());
          }
           executorService.shutdown();
     }
}