[concurrency-interest] Waiting for a cancelled Future
daniel.tetlow at optasense.com
Thu Apr 21 07:59:12 EDT 2016
When I submit a task to one of the standard JDK ExecutorServices, I get a Future that represents the asynchronous execution of that task.
If I cancel the task before it gets executed, then of course Future.get() returns immediately as there is no asynchronous execution to wait for.
If I try to cancel the task while it is executing, what surprises me is that Future.get() still returns immediately even if the task is still actually running. So it appears that upon cancellation, the future becomes detached from the asynchronous execution state of the task. It may be that the task does not cancel gracefully, or that the task needs to do some cleanup on its cancellation path that takes time. In any case, if a task is executing by the time I cancel it, I'd still like to wait for it to finish before proceeding.
I have tried putting my own execution status on the actual Runnable/Callable that I submit to the executor, so I can check that separately. But this seems wrong to me because I end up duplicating status already known by the FutureTask, and this is likely also prone to race conditions.
How should I handle this, or am I doing something wrong to get into this state in the first place?
Thanks for your comments,
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Concurrency-interest