I am using an ExecutorService (a ThreadPoolExecutor) to run (and queue) a lot of tasks. I am attempting to write some shut down code that is as graceful as possible.
Exe
I ran into this issue recently. There may be a more elegant approach, but my solution is to first call shutdown()
, then pull out the BlockingQueue
being used by the ThreadPoolExecutor
and call clear()
on it (or else drain it to another Collection
for storage). Finally, calling awaitTermination()
allows the thread pool to finish what's currently on its plate.
For example:
public static void shutdownPool(boolean awaitTermination) throws InterruptedException {
//call shutdown to prevent new tasks from being submitted
executor.shutdown();
//get a reference to the Queue
final BlockingQueue blockingQueue = executor.getQueue();
//clear the Queue
blockingQueue.clear();
//or else copy its contents here with a while loop and remove()
//wait for active tasks to be completed
if (awaitTermination) {
executor.awaitTermination(SHUTDOWN_TIMEOUT, TimeUnit.SECONDS);
}
}
This method would be implemented in the directing class wrapping the ThreadPoolExecutor
with the reference executor
.
It's important to note the following from the ThreadPoolExecutor.getQueue() javadoc:
Access to the task queue is intended primarily for debugging and monitoring. This queue may be in active use. Retrieving the task queue does not prevent queued tasks from executing.
This highlights the fact that additional tasks may be polled from the BlockingQueue
while you drain it. However, all BlockingQueue
implementations are thread-safe according to that interface's documentation, so this shouldn't cause problems.