views:

20

answers:

1

There are two properties with the following getters (and accompanying setters) in ScheduledThreadPoolExecutor:

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.

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

Also, how are you supposed to change/configure them if you just use the Executors static methods to create instances of ScheduledExecutorService (which doesn't have methods to tweak the above properties)?

+1  A: 

First:

getContinueExistingPeriodicTasksAfterShutdownPolicy()

If that returns false then when you shut down the executor, the executor will cancel/suppress all periodic tasks. When running a periodic task the executor can allow them to finish. Setting this to true will tell the executor to allow all running periodic tasks to finish.

Second:

getExecuteExistingDelayedTasksAfterShutdownPolicy()

There are some tasks that are run with an initial delay but not periodically. This determines if the executor service should cancel the those non-period tasks when shutting down (defaults to true).

Note that a period task is one scheduled with scheduleAtFixedRate while the non-periodic are scheduled with scheduleWithFixedDelay.

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

You would want to change them in the instance you have tasks that must run regardless of shutting down or not.

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

Generally speaking, if you want to control that type of functionality you would have to create your own ScheduledThreadPoolExecutor. If you look at the source of Executors.newScheduledThreadPool you can just use that and handle from there.

    ScheduledThreadPoolExecutor l = new ScheduledThreadPoolExecutor(1);
    l.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
John V.
thanks for the answer. It just seems weird, why the default behavior is to have the Delayed tasks allowed to run at when the executor is `shutdown()` but the periodic tasks are not.
Jason S