Vincent
Vincent

Reputation: 2404

ExecutorService with Task chain shutdown

I have an ExecutorService which executes tasks which itself submit new tasks to the ExecutorService.

When I call .shutdown(), I want the running tasks still being able to submit new tasks to the ExecutorService which need to be finished. But I don't want to be able to submit new tasks from outside to the ExecutorService.

How can I still allow tasks to submit subtasks when the ExecutorService is shutting down?

Upvotes: 2

Views: 799

Answers (2)

jmiserez
jmiserez

Reputation: 3109

Take a look at the ForkJoinPool, which is also an ExecutorService. From within ForkJoinTasks, you can call ForkJoinPool.fork() instead of the standard execute(). When shutting down, fork()s are still be allowed while execute()s are not. However, due to various other differences in behavior, ForkJoinPool might not be suitable for the task at hand (multiple queues instead of one, work-stealing, etc.) Take a look at the documentation here: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinPool.html

Upvotes: 0

zeller
zeller

Reputation: 4974

After you called shutdown, you shouldn't submit any new tasks, it's against the logic.
Use a different executor service for the inner tasks. Or create an executor for yourself (that wraps or extends an executor of your choice) that is capable of checking the submitter and based on its state it either allows or not allows the task submission.

Upvotes: 2

Related Questions