How to force terminate all workers in ThreadPoolExecutor immediately

deadlock picture deadlock · Oct 10, 2012 · Viewed 23.3k times · Source

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

Peter Lawrey picture Peter Lawrey · Oct 10, 2012

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.