ExecutorService 用例

 

 

[java] view plain copy
 
  1. import java.util.concurrent.ExecutorService;   
  2. import java.util.concurrent.Executors;   
  3.   
  4. public class TestCachedThreadPool{   
  5.     public static void main(String[] args){   
  6.         ExecutorService executorService = Executors.newCachedThreadPool();   
  7. //      ExecutorService executorService = Executors.newFixedThreadPool(5);  
  8. //      ExecutorService executorService = Executors.newSingleThreadExecutor();  
  9.         for (int i = 0; i < 5; i++){   
  10.             executorService.execute(new TestRunnable());   
  11.             System.out.println("************* a" + i + " *************");   
  12.         }   
  13.         executorService.shutdown();   
  14.     }   
  15. }   
  16.   
  17. class TestRunnable implements Runnable{   
  18.     public void run(){   
  19.         System.out.println(Thread.currentThread().getName() + "线程被调用了。");   
  20.     }   

 

posted @ 2017-11-21 17:55  搜索技术  阅读(128)  评论(0编辑  收藏  举报