Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixture of shutdown() and shutdownNow()

ScheduledExecutorService inherits two methods from the ExecutorService, shutdown() and shutdownNow(). The difference between them:

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.

Now I want to halt processing of waiting tasks while I don't want to interrupt currently executing tasks. I can't interrupt the threads because third party libraries are involved and they don't deal well with interrupts :-( But I need to cancel scheduled tasks, that are not currently executing since most of them are scheduled in an hour or so.

What's the best way to deal with this? What options Do I have?

like image 818
steffen Avatar asked Mar 14 '16 15:03

steffen


People also ask

What is difference between shutdown () and shutdownNow () in the executor framework?

shutdown() will just tell the executor service that it can't accept new tasks, but the already submitted tasks continue to run. shutdownNow() will do the same AND will try to cancel the already submitted tasks by interrupting the relevant threads.

What does ExecutorService shutdown do?

Two different methods are provided for shutting down an ExecutorService. The shutdown() method will allow previously submitted tasks to execute before terminating, while the shutdownNow() method prevents waiting tasks from starting and attempts to stop currently executing tasks.

What is awaitTermination?

Overview. The awaitTermination method of ExecutorService in Java is used to wait for all the tasks submitted to the service to complete execution: After the shutdown of the service was initiated. The wait time for the termination of the service is over. The current thread of execution is interrupted.

What is invokeAll in Java?

The invokeAll() method of the ExecutorService in Java executes a given list of tasks, and returns a list of Futures that hold the results of all the tasks.


Video Answer


1 Answers

Sounds like calling setExecuteExistingDelayedTasksAfterShutdownPolicy(false) on your executor should do the trick:

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.

Since it's true by default, these tasks are executed. If you set it to false, they shouldn't be executed any more. This shouldn't be confused with submitted tasks which the docs you quote refer to. They are just waiting in the queue to be executed right away, when there is a free worker.

like image 142
Sergei Tachenov Avatar answered Sep 27 '22 19:09

Sergei Tachenov