ExecutorService 用例

Posted 心想事成

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.     }   

 

以上是关于ExecutorService 用例的主要内容,如果未能解决你的问题,请参考以下文章

ExecutorService.execute()

ExecutorService中submit和execute区别

多线程ExecutorService中submit和execute区别

ExecutorService中submit()和execute()的区别

ExecutorService中submit和execute的区别

ExecutorService 用例