newCachedThreadPool无上限线程池使用


1. newCachedThreadPool无上限线程池,   动态根据代码添加线程,   如果线程空闲60秒没有被使用,会自动关闭

 1 package ThreadTest;
 2 
 3 import java.util.concurrent.ExecutorService;
 4 import java.util.concurrent.Executors;
 5 
 6 public class Demo01 {
 7     public static void main(String[] args) {
 8         MyThread myThread = new MyThread();
 9         ExecutorService pool = Executors.newCachedThreadPool();
10         for(int i=0; i<40; i++) {
11             pool.execute(myThread);
12         }
13         pool.shutdown();
14     }
15 }
16 
17 class MyThread implements Runnable{
18     @Override
19     public void run() {
20         for(int i=0; i<30; i++) {
21             System.out.println(Thread.currentThread().getName());
22         }
23     }
24     
25 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM