- workerCount 有效的线程数
-
线程池的运行状态(生命周期)
RUNNING 接受新任务 && 执行队列任务
- SHUTDOWN 不在接受新任务,但是依旧处理队列任务
- STOP 不在接受新任务,不在处理队列任务,中断正在处理的任务
- TIDYING 所有任务已经停止,workerCount为0 ,线程状态变更为TIDYING,将会调用hook的terminated()方法
- TERMINATED terminated() 调用完成
流转状态

GC无法回收ThreadPoolExecutor
public static void testGC() throws InterruptedException {final ThreadPoolExecutor executorService =new ThreadPoolExecutor(2, 2,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),new ThreadFactory() {@Overridepublic Thread newThread(@NotNull Runnable r) {Thread thread = new Thread(r);thread.setName("fff-" + System.currentTimeMillis());thread.setDaemon(true);return thread;}});executorService.submit(() -> System.out.println("gggod"));executorService.submit(() -> System.out.println("gggod"));System.gc();}

由于线程Thread依旧引用了ThreadPoolExecutor,尽管ThreadPoolExecutor已经不在方法的上下文当中了,它却依旧无法被回收。
因此应该尽力避免出现在方法中new ThreadPoolExecutor的情况,即使一定需要,那也要调用其shutdown方法,方便后续回收。
