关闭核心线程

188 阅读1分钟
public class ThreadPoolTest {
    public static void main(String[] args) {
        // 这里把corePoolSize设为5,keepAliveTime保持不变
        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 5,
                30L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(15));
        // 允许核心线程超时销毁
        executor.allowCoreThreadTimeOut(true);
        for (int i = 0; i < 20; i++) {
            executor.execute(() -> {
                System.out.println(Thread.currentThread().getName());
            });
        }
    }
}