Futuer#cancel(boolean)
javadoc states
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.
So either the ExecutorService
hasn't executed the Runnable
/Callable
and can remove it from its queue or it has and the ExecutorService
has to call Thread#interrupt()
and your Runnable
/Callable
code has to handle it.
There's no other way through the Future
interface to interrupt a thread. If your Runnable
doesn't have a way to handle interrupts, then a number of things can happen. If an InterruptedException
occurs, it will bubble up to the ExecutorService
and be wrapped in a ExecutionException
that will be thrown from Future#get()
. If no InterruptedException
occurs in the thread executing the Runnable
, your Runnable
will continue unhindered.
You should really consider changing your Runnable
to handle interrupts.