java 线程池 ---- newCachedThreadPool()
class MyThread implements Runnable{ private int index; public MyThread(int index){ this.index = index; } @Override public void run() { System.out.println("处理任务:" + index); } } public class Test1 { public static void main(String[] args){ // 创建线程池 ExecutorService executor = Executors.newCachedThreadPool(); for (int i = 0; i < 15; i++){ MyThread myThread = new MyThread(i); // 任务丢进线程池 executor.execute(myThread); } // 不需要关闭服务, 如果线程空闲 60 秒, 将会自动销毁; 而核心线程数设置为 0, 所以 60s 后会销毁所有线程 } }