If awaitTermination() returns false in your example, you can try calling shutdownNow() . This method will do its best to cancel all tasks that are still in progress, but it does not guarantee anything. Some poorly implemented tasks may not have a cancellation policy and just work forever. In this case, the threads will never be stopped, and the performer will never collect garbage.
Such tasks will also prevent the graceful termination of your program (unless you mark your workflows as daemons).
For example, if your task contains an empty infinite loop, it will not be canceled, even if you call shutdownNow() .
There may also be times when a task does not have the correct cancellation policy, and works for too long (but not forever). For example, he has a very long empty loop. You may not be able to close the pool that performs this task with shutdown() / shutdownNow() , but sooner or later it will complete its work and the thread will be terminated with the executor.
source share