[concurrency-interest] backport: execution service shutdownNow question

Tom Sugden tom at epcc.ed.ac.uk
Tue Mar 7 06:25:29 EST 2006


Hello,

I'm new to the concurrency utils, so I'm not sure whether I've found a bug
in the backport or just misunderstood the APIs. Here is my scenario:

I have used an execution service to invoke a collection of Callable objects
which block indefinitely. I then call shutdownNow() on the service from
another thread. Here is some simplified code:

    // from one thread:
    ExecutorService service = Executors.newCachedThreadPool();
    Collection activityProcessors = new ArrayList();
    activityProcessors.add(new ActivityProcessor("activity1"));
    activityProcessors.add(new ActivityProcessor("activity2"));
    List futureResults = service.invokeAll(activityProcessors);

    // later, from another thread:
    service.shutdownNow();

Afterwards I check the cancelled status on the Future objects associated
with each of the Callable objects. I would have expected isCancelled() to
return true but instead it returns false. Is this to be expected even though
the Callable objects have been stopped at an intermediate stage due to the
shutdownNow() call? Would I have been better to use the submit() method
instead of invokeAll(), then cancelled the tasks via a number of calls to
Future#cancel(true)? Is there a better way to cancel multiple tasks?

Many thanks for any help,

Tom Sugden



More information about the Concurrency-interest mailing list