deadlock deadlock - 1 month ago 12
Java Question

How to force terminate all workers in ThreadPoolExecutor immediately

I have a number of tasks inside a

TheadPoolExecutor
. I have a stop button on my interface that should terminate all the threads inside
ThreadPoolExecutor
immediately. I am looking for a way to do it. (without
shutDown()
or
shutDownNow()
).

Thanks

Answer

You cannot safely kill threads immediately. You tasks should instead honour interrupts and stop when interrupted. If you use ThreadPoolExecutor.shutdownNow(), all the running tasks will be interrupted.

The only alternative is to the threads in a separate process is issue a signal to kill the process.