views:

64

answers:

1

I have implemented custom cancellation logic as described in Concurrency in Practice.

Encapsulating nonstandard cancellation in a task with newTaskFor.

This works fine and I can call cancel on the a future and the task is cancelled as expected. I need to be able to destroy my executor service which i am using by calling "shutdownNow" method, but this method simply calls interupt on the threads which means my custom cancel logic is never called. As my tasks use non-blocking sockets interrupting the thread does not work hence why i have custom cancellation logic.

Is there a simple solution to cancel all the tasks in progress. I have tried overwriting the shutdown method on ThreadPoolExecutor but I don't have access to the list of workers. I would like to be able to cancel everything by shutting down the executor as its used in several places to submit tasks, is there a simple solution to this?

A: 

As John pointed out, it would be best if you can make your tasks interruptible. Then you can simply rely on the ThreadPoolExecutor to interrupt all worker threads to have an orderly cancellation.

If that is not possible, you can add additional logic in ThreadPoolExecutor to achieve what you want. It is somewhat involved, and might not be pretty to some (and may hurt performance a bit), but I think it'll get the job done. I think basically you need to maintain a list of active tasks yourself. The key is to override the beforeExecute() and the afterExecute() methods:

public class MyExecutor extends ThreadPoolExecutor {
    private final Queue<RunnableFuture> activeTasks = 
            new LinkedBlockingQueue<RunnableFuture>();
    ...

    protected void beforeExecute(Thread t, Runnable r) {
        RunnableFuture task = (RunnableFuture)r;
        activeTasks.add(task);
    }

    protected void afterExecute(Thread t, Runnable r) {
        RunnableFuture task = (RunnableFuture)r;
        activeTasks.remove(task);
    }

    public void cancelAllActiveTasks() {
        for (RunnableFuture f: activeTasks) {
            f.cancel(true); // invokes your custom cancellation logic
        }
    }

You can call cancelAllActiveTasks() or override shutDownNow() to call it as well. One thing I do not like about this is having to remove the task from the queue as it's not a constant-time operation.

sjlee
Point taken. One could choose a different collection, like a LinkedBlockingQueue for example. Remove() is not pretty in many ways. :)
sjlee
Yea sorry was clicking around and accidentally deleted the comment
John V.
edited the code not to use the ConcurrentLinkedQueue.
sjlee
this is the conclusion i came to and have implemented similar for now. Will see if there are any other solution or will accept this answer.thanks
crafty