shutdown()與awaitTermination()的分析

  • shutdown()
    源碼說明如下:

/**
* Initiates an orderly shutdown in which previously submitted
* tasks are executed, but no new tasks will be accepted.
* Invocation has no additional effect if already shut down.
*
*

This method does not wait for previously submitted tasks to
* complete execution. Use {@link #awaitTermination awaitTermination}
* to do that.
*

上面的意思是說:在先前提交的任務(就是run中跑的東西)被執行的時候,開始有序的關閉。新的任務不會被執行。如果已關閉,則調用沒有其他效果。該方法不會等待先前已提交的任務完全執行。

  • awaitTermination()
 awaitTermination(long timeout, TimeUnit unit)

第一個參數timeout是等待關閉線程池執行器的時間大小,第二個參數TimeUnit是時間的粒度,也就是以多大的粒度(小時,分鐘,秒)來設置第一個參數的單位。

/**
* Returns true if this executor is in the process of terminating
* after {@link #shutdown} or {@link #shutdownNow} but has not
* completely terminated. This method may be useful for
* debugging.
A return of {@code true} reported a sufficient
* period after shutdown may indicate that submitted tasks have
* ignored or suppressed interruption, causing this executor not
* to properly terminate.
*
* @return {@code true} if terminating but not yet terminated
*/

等待執行器完全關閉,返回爲真。

  • 總結
    shutdown()關閉任務,awaitTermination關閉執行器(executor)。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章