Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何stop关闭线程池任务? #2

Open
yangchong211 opened this issue May 23, 2019 · 1 comment
Open

如何stop关闭线程池任务? #2

yangchong211 opened this issue May 23, 2019 · 1 comment

Comments

@yangchong211
Copy link

No description provided.

@yangchong211
Copy link
Author

可以直接在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();
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant