Java ServiceExecutor terminating condition

家住魔仙堡 提交于 2019-12-01 23:33:56

Executor will keep running with it under lying thread pool. you can still execute or submit a new task. It is recommended to shutdown Executor service call ExecutorService.html#shutdown() which attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.

You can also use ExecutorService.html#shutdownNow() which stops all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. It is useful when you need immediate shutdown.

1) threads in a fixed thread pool executor never terminate once started

2) there is no master thread in thread pool executor

you can think of it that way:

shutdown() will just tell the executor service that it can't accept new tasks, but the already submitted tasks continue to run

shutdownNow() will do the same AND will try to cancel the already submitted tasks by interrupting the relevant threads. Note that if your tasks ignore the interruption, shutdownNow will behave exactly the same way as shutdown.

Use shutdown method to gracefully shutdown from interface ExecuterService, as Quoi suggested it still runs even if threads are done. You can use submit anytime after that for new work. Other utility methods are here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!