简体   繁体   中英

Shutdown Executor thread after awaitConfimation

How to shutdown all threads created with ExecutorService after the

executor.awaitTermination(1, TimeUnit.DAYS);

is finished?

That's the max time threads can work, and if, in some case, any of them does not finish current task, shut it down and start again.

If awaitTermination returns true, then all the threads have been shutdown.

If awaitTermination returns false, then some of the threads are obviously not responding to interruption*, and apart from exiting the java process, there is not much you can do...

*If the tasks run by the threads are yours, make sure they respond to interruption by exiting quickly and awaitTermination will return promptly.

There is no easy way to do it. You can use a ThreadFactory which will create daemon threads, then run awaitTermination in the main thread, followed by shutdownNow . This will (hopefully) shut down the whole JVM, and there is little to nothing better than that in the given situation. Executing System.exit() is an even more drastic measure.

Note that interrupting or even stopping threads doesn't guarantee that they will actually terminate.

You can call System.exit(1) to stop the application. If you dont want to exit then you should design your tasks so that they react to Thread.interrupt properly, there is no other way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM