From java docs on Future.cancel()
boolean cancel(boolean mayInterruptIfRunning)
Attempts to cancel execution of this task. This
My question is what does cancel do if mayInterruptIfRunning is false? how does it cancel or stop the task from executing if it has already been run?
If the task has already started running, and mayInterruptIfRunning
is false
, then there is nothing to be done. In Java, interrupting a thread is considered to be the only safe way of stopping it short of completion - and even that requires the task to "comply" by checking for interruption at some implementation-specific interval.
Related: How do you kill a thread in Java?