gpt4 book ai didi

java - shutdown() 和 shutdownNow() 的混合

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:22:18 24 4
gpt4 key购买 nike

ScheduledExecutorServiceExecutorService 继承了两个方法, shutdown()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,则不应再执行它们。这不应与您引用的文档引用的已提交 任务混淆。他们只是在队列中等待有空 worker 时立即执行。

关于java - shutdown() 和 shutdownNow() 的混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35991183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com