美文网首页
ExecutorService的典型关闭方法

ExecutorService的典型关闭方法

作者: 大风过岗 | 来源:发表于2020-10-22 13:42 被阅读0次

关闭ExecutorService的方法

private shutdownExecutorService(){

    //1、停止线程池接收新任务
    //2、等待一定时间,让现存的任务执行结束
    //3、取消当前运行的任务
    //4、如果当前线程也被中断的话,那么就再次关闭线程池,同时恢复中断状态
    pool.shutdown();
    try{
       if(!pool.awaitTermination(60,TimeUnit.SECONDS)){
            pool.shutdownNow();
            if(!pool.awaitTermination(60,TimeUnit.SECONDS)){
                System.out.println("Pool did not terminate");
            }           
       }
    }catch(InterruptedException ie){
         pool.shutdownNow();
         Thread.currentTread().interrupt():
    }
    
}

相关文章

网友评论

      本文标题:ExecutorService的典型关闭方法

      本文链接:https://www.haomeiwen.com/subject/lvwumktx.html