Executor Service的shutdown和shutdownNow之间的区别 [英] Difference between shutdown and shutdownNow of Executor Service

查看:270
本文介绍了Executor Service的shutdown和shutdownNow之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道关闭<$ shutdown() shutdownNow()之间的基本区别c $ c>执行者服务?据我所知 shutdown()应该用于 graceful 关闭,这意味着应该允许所有正在运行和排队等待处理但未启动的任务完成并且 shutdownNow() 突然关闭意味着一些未完成的任务任务被取消,未启动的任务也被取消。还有其他隐含/明确我缺少的吗?

I want to know the basic difference between shutdown() and shutdownNow() for shutting down the Executor Service ?As far as I understood shutdown() should be used for graceful shutdown which means all tasks that were runing and queued for processing but not started should be allowed to complete and shutdownNow() does an abrupt shut down meaning that some unfinished tasks are cancelled and unstarted tasks are also cancelled . Is there anything else which is implicit/explicit that I am missing ?

PS:我在 SO 与此相关但不完全是我想知道的。

P.S: I found another question on SO related to this but not exactly what I want know .

推荐答案

总之,你可以这样想:


  • shutdown()只会告诉执行程序服务它不能接受新任务,但已经提交的任务继续运行

  • shutdownNow()将执行相同操作并通过中断相关线程尝试取消已提交的任务。请注意,如果您的任务忽略了中断, shutdownNow 的行为方式与 shutdown 完全相同。

  • shutdown() will just tell the executor service that it can't accept new tasks, but the already submitted tasks continue to run
  • shutdownNow() will do the same AND will try to cancel the already submitted tasks by interrupting the relevant threads. Note that if your tasks ignore the interruption, shutdownNow will behave exactly the same way as shutdown.

您可以尝试下面的示例,并通过 shutdownNow替换 shutdown 更好地理解不同的执行路径:

You can try the example below and replace shutdown by shutdownNow to better understand the different paths of execution:


  • shutdown ,输出在100ms后仍在等待:调用System.exit(0)... 因为正在运行的任务中断并继续运行。

  • shutdownNow ,输出中断正常退出... 因为正在运行的任务被中断,捕获中断然后停止它正在做的事情(打破while循环)。

  • 使用 shutdownNow ,如果你注释掉while循环中的行,你将得到仍在100ms之后等待:调用System.exit(0).. 。因为运行不处理中断ning任务更长。

  • with shutdown, the output is Still waiting after 100ms: calling System.exit(0)... because the running task is not interrupted and continues to run.
  • with shutdownNow, the output is interrupted and Exiting normally... because the running task is interrupted, catches the interruption and then stops what it is doing (breaks the while loop).
  • with shutdownNow, if you comment out the lines within the while loop, you will get Still waiting after 100ms: calling System.exit(0)... because the interruption is not handled by the running task any longer.
public static void main(String[] args) throws InterruptedException {
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        @Override
        public void run() {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("interrupted");
                    break;
                }
            }
        }
    });

    executor.shutdown();
    if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) {
        System.out.println("Still waiting after 100ms: calling System.exit(0)...");
        System.exit(0);
    }
    System.out.println("Exiting normally...");
}

这篇关于Executor Service的shutdown和shutdownNow之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆