Is there any way to interrupt a Future without cancelling it?
boolean cancel (boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. This attempt will fail if the task has already completed, has already been cancelled, or could not be cancelled for some other reason. If successful, and this task has not started when cancel is called, this task should never run. If the task has already started, then the mayInterruptIfRunning parameter determines whether the thread executing this task should be interrupted in an attempt to stop the task.
To capture the interrupt, we have to properly catch the Interrupted Exception or check the isInterrupted() method within the Runnable / Callable method.
But there is no way to interrupt a running Future using the Future interface
Since all the threads are in the Executor Service pool, no one can do thread.interrupt(). Is that why it has been assumed that any interrupt will come only when a Future is cancelled or a Thread Pool is terminating?
I am trying to understand why there is not a interrupt method in the Future interface. Any help will be greatly appreciated
Future.cancel()
method does useThread.interrupt()
under the hood, so i don't understand your attempt to distinguish the 2. are you confusingThread.interrupt()
withThread.stop()
? – CoremakerFuture.cancel()
/Thread.interrupt()
instead of using dangerousThread.stop()
method, which can cause very unexpected side effects. Thanks! :) – SelfdelusionFuture
started and watched a new thread. Then, you could respond to the normal interrupt flag by stopping the second thread. So, in this twisted way, you could get the control back in a Future setting. I'm not arguing here if it's good or bad, only showing it's possible. – Selfdelusion