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 }