gpt4 book ai didi

java - ScheduledThreadPoolExecutor + setXXXXShutdownPolicy()

转载 作者:行者123 更新时间:2023-11-30 05:09:39 25 4
gpt4 key购买 nike

ScheduledThreadPoolExecutor 中有两个属性具有以下 getter(以及随附的 setter) :

boolean getContinueExistingPeriodicTasksAfterShutdownPolicy()

Get the policy on whether to continue executing existing periodic 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 false.

boolean getExecuteExistingDelayedTasksAfterShutdownPolicy()

Get 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.

为什么它们有不同的默认值,是否有理由更改这些属性的值?

此外,如果您只使用Executors,您应该如何更改/配置它们用于创建 ScheduledExecutorService 实例的静态方法(它没有调整上述属性的方法)?

最佳答案

第一:

getContinueExistingPeriodicTasksAfterShutdownPolicy()

如果返回 false,那么当您关闭执行器时,执行器将取消/抑制所有定期任务。当运行周期性任务时,执行器可以允许它们完成。将其设置为 true 将告诉执行器允许所有正在运行的定期任务完成。

第二:

getExecuteExistingDelayedTasksAfterShutdownPolicy()

有些任务的运行有初始延迟,但不是定期运行。这决定了执行器服务在关闭时是否应该取消那些非周期任务(默认为 true)。

请注意,周期任务是使用 scheduleAtFixedRate 计划的,而非周期任务是使用 scheduleWithFixedDelay 计划的。

Why do they have different default values, and is there ever a reason to change the value of these properties?

如果您有无论关闭与否都必须运行的任务,您可能希望更改它们。

how are you supposed to change/configure them if you just use the Executors static methods to create instances of ScheduledExecutorService

一般来说,如果您想控制该类型的功能,您必须创建自己的 ScheduledThreadPoolExecutor。如果您查看 Executors.newScheduledThreadPool 的源代码,您可以使用它并从那里进行处理。

    ScheduledThreadPoolExecutor l = new ScheduledThreadPoolExecutor(1);
l.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);

关于java - ScheduledThreadPoolExecutor + setXXXXShutdownPolicy(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3893609/

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