关于线程池的一些用法

1、获取线程池中线程数量

int threadCount = ((ThreadPoolExecutor)timerPool).getActiveCount();

2、优雅的关闭线程池

​ 注:在命名用shutdown的时候,需要使用awaitTermination的方法进行关闭阻塞。下面的例子是尝试关闭两次,每次时间为2秒

public void cannel(){
    this.isRunnable = false;
    threadPool.shutdown();
    try{
        if(!threadPool.awaitTermination(2, TimeUnit.SECONDS)){
            threadPool.shutdownNow();
            if(!threadPool.awaitTermination(2,TimeUnit.SECONDS)){
                log.error("thread poll not finish !!!");
            }
        }
    }catch (Exception e){
        log.error("close thread pool error",e);
    }
}