shutdown() 和 shutdownNow() 的混合
Mixture of shutdown() and shutdownNow()
ScheduledExecutorService inherits two methods from the ExecutorService, shutdown() and shutdownNow()。它们之间的区别:
shutdown
initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.
shutdownNow
attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
现在我想停止处理等待中的任务,同时又不想中断当前正在执行的任务。我不能中断线程,因为涉及第三方库并且它们不能很好地处理中断:-(但是我需要取消 scheduled 任务,这些任务当前没有执行,因为它们中的大多数都安排在一个小时左右。
处理此问题的最佳方法是什么?我有什么选择?
听起来像在你的遗嘱执行人上调用 setExecuteExistingDelayedTasksAfterShutdownPolicy(false) 应该可以解决问题:
Sets the policy on whether to execute existing delayed tasks even when
this executor has been shutdown. In this case, these tasks will only
terminate upon shutdownNow, or after setting the policy to false when
already shutdown. This value is by default true.
因为默认是true
,所以会执行这些任务。如果将其设置为 false
,则不应再执行它们。这不应与您引用的文档引用的 submitted 任务混淆。他们只是在队列中等待有空闲工作人员时立即执行。
ScheduledExecutorService inherits two methods from the ExecutorService, shutdown() and shutdownNow()。它们之间的区别:
shutdown
initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.
shutdownNow
attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
现在我想停止处理等待中的任务,同时又不想中断当前正在执行的任务。我不能中断线程,因为涉及第三方库并且它们不能很好地处理中断:-(但是我需要取消 scheduled 任务,这些任务当前没有执行,因为它们中的大多数都安排在一个小时左右。
处理此问题的最佳方法是什么?我有什么选择?
听起来像在你的遗嘱执行人上调用 setExecuteExistingDelayedTasksAfterShutdownPolicy(false) 应该可以解决问题:
Sets the policy on whether to execute existing delayed tasks even when this executor has been shutdown. In this case, these tasks will only terminate upon shutdownNow, or after setting the policy to false when already shutdown. This value is by default true.
因为默认是true
,所以会执行这些任务。如果将其设置为 false
,则不应再执行它们。这不应与您引用的文档引用的 submitted 任务混淆。他们只是在队列中等待有空闲工作人员时立即执行。