Reputation: 1305
I am using a single ScheduledThreadPoolExecutor
to manage multiple tasks and would like to add the option to stop them.
I understand the difference between shutdown and shutdownNow on a regular executor service, and am interested in understanding how it will affect tasks in a ScheduledThreadPoolExecutor
- where tasks are also scheduled to be run on a regular interval.
Given a running ScheduledThreadPoolExecutor
that has both running tasks, one time tasks schedled to be run in the future, and tasks that are scheduled to be run in a constant interval (once every X minutes).
What will be the best way to implement the following "commands" :
Upvotes: 2
Views: 1357
Reputation: 925
You can use the method :
public List<Runnable> shutdownNow()
, but please note that the currently executing threads may not immediately terminate. If you want to do that, then you have to find a way to signal a killing of the process via a separate thread or something.From the Javadocs: There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. This implementation cancels tasks via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.
This can be achieved by using the method:
public void shutdown()
From the Javadocs: If the ExecuteExistingDelayedTasksAfterShutdownPolicy has been set false, existing delayed tasks whose delays have not yet elapsed are cancelled. And unless the ContinueExistingPeriodicTasksAfterShutdownPolicy has been set true, future executions of existing periodic tasks will be cancelled.
Upvotes: 4
Reputation: 262494
You can configure this using
So your two cases are
shutdownNow
shutdown
Upvotes: 1