[concurrency-interest] Is cancel method used anywhere?

David Holmes dholmes@dltech.com.au
Wed, 14 Jul 2004 10:43:25 +1000


Mike,

> I guess this is a little late, but I don't see why shutdownNow
> doesn't call cancel first or instead of Thread.interrupt if the
> task implements the Future interface.  It seems to me that the
> cancel method is worthless unless you write your own Executor.

Your Future is just a Runnable in a queue - in fact by the time it is being
executed it is not even in the queue anymore. So when shutdownNow is invoked
the pool doesn't know what kinds of tasks are being executed and doesn't
have a handle to them even if it did know. The pool interrupts the thread
but the thread can't do anything until your tasks code responds to the
interrupt and returns - by which time there is no point doing a cancel.

The cancel method is there for you to cancel your Future's, not for a pool
to cancel them on your behalf.

If you want to do the cancel then track which Future's have not yet
completed and cancel them your self when deciding to shutdown the pool. Upon
completion each custom FutureTask could remove itself from your queue of
incomplete tasks. You could do this in a custom executor.

Hope this helps.

David Holmes