EasyThread icon indicating copy to clipboard operation
EasyThread copied to clipboard

如何stop关闭线程池任务?

Open yangchong211 opened this issue 5 years ago • 1 comments

yangchong211 avatar May 23 '19 03:05 yangchong211

可以直接在EasyThread方法中添加下面代码,既可以关闭任务,经过调试是可以的。

    public void stop(){
        try {
            // shutdown只是起到通知的作用
            // 只调用shutdown方法结束线程池是不够的
            pool.shutdown();
            // (所有的任务都结束的时候,返回TRUE)
            if(!pool.awaitTermination(0, TimeUnit.MILLISECONDS)){
                // 超时的时候向线程池中所有的线程发出中断(interrupted)。
                pool.shutdownNow();
            }
        } catch (InterruptedException e) {
            // awaitTermination方法被中断的时候也中止线程池中全部的线程的执行。
            e.printStackTrace();
        } finally {
            pool.shutdownNow();
        }
    }

yangchong211 avatar May 23 '19 11:05 yangchong211