Concurrency - interrupting a Future without cancelling it Concurrency - interrupting a Future without cancelling it multithreading multithreading

Concurrency - interrupting a Future without cancelling it


The reason is because of the difference in the abstraction that is a Future and the concrete execution in a thread. We cannot say if a future is tied to a single thread or multiple thread. A future may start new threads, start new futures, etc.

Consider these abstractions as interactions between the client code and the executor of the futures. Conceptually it makes sense to say "cancel this task I have asked you to do" because it was your task to cancel. I may be busy working on it, or I may not have started it yet, or it may be finished but that's all fine, I will cancel it if you want me to. So that's why we have a cancel method.

On the other hand, it does not make as much sense to say "interrupt your task". Because of the decoupling between the result of the action (the Future) and the execution model (say an Executor), the client does not have knowledge of what actions are being taken to fulfil the task. How then can the client be expected to know when an interrupt is appropriate, required, or even supported.


At any rate you can use a timeout, so you interrupt your waiting.

get(long timeout, TimeUnit unit) 


There are useful usecases for doing this.

Since the problem with the Future API is that it doesn't provide the capability to detect that the execution has stopped. isCancelled() and isDone() will both happily return true even though execution of the Callable continues.

So it's really not about the level of abstraction of the Future API at this stage. But it's inability to identify task termination. Or to put differently, it's inability to differentiate between a cancel request and a completed cancel action.

One work-around is to use a CountdownLatch as done in the question Waiting for a cancelled future to actually finish . This will signal to the waiting caller that the task is actually done, and not just signaled to stop.