Future task of ExecutorService not truly cancelling

I later hit breakpoints within the Callable procedure, as if the Future cancel() had no effect.

Future.cancel(true) removes a job that is in the queue and not yet running but if the job is already running it does the equivalent of Thread.interrupt() on the thread running the job. This sets the interrupt bit on the thread and causes any sleep(), wait(), and some other methods to throw InterruptedException.

It is important to realize that it does not stop the thread. You need to actively check for the interrupt flag in your thread loop or properly handle InterruptedException.

See my SO answer here for more details: How to suspend thread using thread’s id?

Leave a Comment