[concurrency-interest] Waiting for a cancelled Future

Vladimir Ozerov ppozerov at gmail.com
Thu Apr 21 08:36:22 EDT 2016


Hi Dan,

This is an interesting question. Some frameworks which have their own
"futures" has the same semantics as you described - they still wait for
computation to complete. And both approaches could make sense.

However, future.get() is essentially about result, not about thread or
computation state. When future is cancelled, result is known immediately,
and there is no reason to block waiters any further. So immediate return
makes perfect sense to me in this case.

Vladimir.
21 апр. 2016 г. 15:09 пользователь "Daniel Tetlow" <
daniel.tetlow at optasense.com> написал:

Hi,



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,



Dan.



_______________________________________________
Concurrency-interest mailing list
Concurrency-interest at cs.oswego.edu
http://cs.oswego.edu/mailman/listinfo/concurrency-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20160421/f14b28c4/attachment.html>


More information about the Concurrency-interest mailing list