With a Java ExecutorService, how do I complete actively executing tasks but halt the processing of waiting tasks?

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, etc. ) while synchronizing on it to prevent polling. Finally, calling awaitTermination() allows the thread pool to finish what's currently on its plate.

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, etc. ) while synchronizing on it to prevent polling. Finally, calling awaitTermination() allows the thread pool to finish what's currently on its plate.

For example: public static void shutdownPool() { //call shutdown to prevent new tasks from being submitted executor.shutdown(); //get a reference to the Queue BlockingQueue blockingQueue = executor.getQueue(); //synchronize on it to prevent tasks from being polled synchronized (blockingQueue) { //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) { try { executor. AwaitTermination(SHUTDOWN_TIMEOUT, TimeUnit. SECONDS); } catch (InterruptedException ie) { //log exception } } } 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 need for the synchronized block to prevent additional tasks being polled from the Queue while you clear or drain it.

I will give this a try. BlockingQueue uses a ReentrantLock for just about every action, so I'm not sure if the synchronized is necessary. – Jeff Goldberg Nov 16 at 21:45 @JeffGoldberg Ah, good point.

I'll have to revisit whether that's needed when I get a chance. This is why I love SO :) – Kublai Khan Nov 16 at 22:02 Not sure what I was talking about with BlockingQueue using a lock. BlockingQueue is an interface so it uses nothing.

I think I was looking at PriorityBlockingQueue. – Jeff Goldberg Nov 16 at 22:23 Ok, so, well, if you use the Executors class to get a newFixedThreadPool it uses a LinkedBlockingQueue, which also has both put and take locks. Anyway, I'll implement this and let you know how it goes.

– Jeff Goldberg Nov 16 at 22:26 One other note: in your sample code, you should call executor.shutdown(); before clearing the queue. Otherwise there's the risk that another thread could add a task the to ExecutorService after you've cleared it but before you've shut it down. Since shutdown simply prevents new tasks from being added, it's safe to call that first.

– Jeff Goldberg Nov 16 at 22:28.

You can wrap each submitted task with a little extra logic wrapper = new Runnable() public void run() if(executorService.isShutdown()) throw new Error("shutdown"); task.run(); executorService. Submit(wrapper); the overhead of extra checking is negligible. After executor is shutdown, the wrappers will still be executed, but the original tasks won't.

1 This is a very good idea. To avoid having to wrap every single Runnable or Callable in your code, you could instead extend the ThreadPoolExecutor (or the ExecutorService of your choice), override the overloaded newTaskFor methods, and wrap all incoming Callable/Runnable tasks with your special "AbortOnShutdownCallable/Runnable". If Kublai Khan's "clear the blocking queue" approach ends up not working, I'll try this.

But either way +1. – Jeff Goldberg Nov 17 at 14:42.

The shutdownNow() is exactly what you need. You've missed the 1st word Attempts and the entire 2nd paragraph of its javadoc: There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.So, only tasks which are checking Thread#isInterrupted() on a regular basis (e.g. In a while (!Thread.currentThread().isInterrupted()) loop or something), will be terminated.

But if you aren't checking on that in your task, it will still keep running.

My concern about this approach is there are times when I do actually want to interrupt active tasks. They currently do check interrupted status and handle InterruptedExceptions properly, because in emergencies (or at the end of a daily cycle) I need to shut everything down immediately. – Jeff Goldberg Nov 16 at 21:07 Add an extra check next to the interrupted status whether there's means of an emergency state.

– BalusC Nov 16 at 21:10 In addition to Thread.isInterrupetd(), there are other interruption points, including Thread.sleep(), Thread.join(), and Object.wait(). – John Haager Nov 17 at 0:32 @John: Yes, the ones throwing InterruptedException. It's however very rare to use any of them in an executor task.

– BalusC Nov 17 at 0:35 @BalusC: I think this is a valid and good answer, but I disagree that it's very rare to find InterruptedException cases in an executor task. My tasks definitely deal with them--there's a lot of threads which are pausing as they wait for third party activities so as to free up resources for other threads. In fact, I don't understand why you wouldn't always expect that behavior.

The InterruptedException seems especially designed for the situation where you have multiple threads being run at one time by an executor. – Jeff Goldberg Nov 17 at 14:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions