We must be sure that the current set of tasks submitted to the executor must be completed and shall not get killed in the process. But we cannot shutdown the executor just like that. This behavior is helpful in the case of web/desktop applications that perform these tasks for the infinite duration on basis of when new tasks arrive.īut when we have an application that does not use the executor very frequently – and we want to reclaim the memory for other components to use – we must shutdown the executor. It simply means that JVM will not terminate if we are expecting it to. It will stay alive and wait for new tasks to come. In general, the ExecutorService will not be automatically destroyed when there is no task to process. Using shutdown() and awaitTermination() 2.1 When to Use Remember that awaitTermination() is invoked after a shutdown() request.Ģ. The awaitTermination(long timeout, TimeUnit unit) blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first. This implementation cancels tasks via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate. There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. This method does not wait for actively executing tasks to terminate and tries to stop them forcefully. The shutdownNow(), forcibly, attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. This method does not wait for previously submitted tasks (but not started executing) to complete execution. The shutdown() initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. void shutdown() īoolean awaitTermination(long timeout, TimeUnit unit) Let us start with checking out the syntax of these methods. Difference between shutdown(), shutdownNow() and awaitTermination() Using shutdown() and awaitTermination()ġ.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |