父接口:bash
子接口:網絡
實現類:異步
ExecutorService提供了一些方法,包括管理取消任務和可使用Future來追蹤那些異步執行的任務結果。ExecutorService是能夠被關閉的,固然這會致使它沒法接受新的任務。
ExecutorService提供了兩個不一樣的方法來關閉本身:socket
接近終止時,executor沒有任何激活的任務,沒有任何等待執行的任務,也不會有新的任務提交。一個再也不使用的ExecutorService應該被關閉,這樣能夠回收部分資源。post
submit方法調用了父接口的Executor.execute(Runnable)方法,建立了一個Future對象,能夠用來取消或等待計算結束。invokeAny和invokeAll方法主要用於批量執行任務的場景,批量執行一批任務,分別等待至少一個任務結束或者所有任務結束。對於這些方法,ExecutorCompletionService 能夠用來寫一些自定義的變種。學習
Executors方法提供了一些工廠方法相關的工廠方法。測試
用例: 下面的例子使用線程池處理收到的請求,這實際上是一個簡易的網絡服務。它使用了預約義的工廠方法: Executors.newFixedThreadPool(int)
ui
class NetworkService implements Runnable {
private final ServerSocket serverSocket;
private final ExecutorService pool;
public NetworkService(int port, int poolSize)
throws IOException {
serverSocket = new ServerSocket(port);
pool = Executors.newFixedThreadPool(poolSize);
}
public void run() { // run the service
try {
for (;;) {
pool.execute(new Handler(serverSocket.accept()));
}
} catch (IOException ex) {
pool.shutdown();
}
}
}
class Handler implements Runnable {
private final Socket socket;
Handler(Socket socket) { this.socket = socket; }
public void run() {
// read and service request on socket
}
}
複製代碼
以下的方法經過兩步來關閉ExecutorService,第一步調用shutdown來拒收新的任務,而後調用shutdownNow,若是須要的話,取消全部處於等待中的任務:this
void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
pool.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
}
}
複製代碼
內存一致性影響:在將Runnable或Callable任務提交給ExecutorService以前,線程中的操做發生在該任務執行的任何操做以前,而該操做又發生在經過Future.get()檢索結果以前。spa
關閉本身以前會等待全部任務都執行完畢,此時方法會一直阻塞,直到阻塞時間大於超時時間,或者當前線程被中斷
Parameters: timeout - 等待最大時長
unit - 等待時間單位
Returns:
Throws:
InterruptedException - 執行時線程被中斷
執行給定的批量任務,當全部任務都執行完畢時會返回一個包含全部任務結果Future的list集合。集合中的全部元素的Future.isDone()方法的調用結果都是true。可是要注意,一個已完成的任務多是正常結束,也多是拋了異常。若是給定的任務集合在任務執行中被更改,可能會致使這個方法的返回值可能會變成未定義的。因此任務列表在提交到這個方法時不要作任何的修改!
Type Parameters:
T - the type of the values returned from the tasks
Parameters:
tasks - 任務列表
Returns:
一個Future集合用來表示任務的執行結果,集合順序與給定的任務集合是一致的,並且集合中的future的狀態都是已完成
Throws:
InterruptedException - 若是等待時被中斷,這回致使未執行的任務會被取消
NullPointerException - 若是入參任務集合中的某個元素是null
RejectedExecutionException - 如何任意的任務沒法被執行
此方法和上面的方法基本是同樣的,惟一的區別是:上面的方法執行完全部給定任務後才返回,此方法是執行全部任務,可是若是執行時間超過了超時時間,也會強制返回結果
Type Parameters:
T - the type of the values returned from the tasks
Parameters:
tasks - 任務列表
timeout - 超時時間
unit - 時間單位
Returns:
與上面的方法惟一的區別在於,若是執行批量任務沒有報超時,那麼返回結果的全部future都是已完成狀態,不然會存在一些未完成的future
Throws: InterruptedException - 若是等待時被中斷,這回致使未執行的任務會被取消
NullPointerException - 若是入參任務集合中的某個元素是null
RejectedExecutionException - 如何任意的任務沒法被執行
執行給定的任務,返回成功完成的某一個任務的結果,任意一個在超時時間以前完成的均可以。在接近返回時(包括正常返回和異常返回),那些沒有完成的任務會被取消。若是給定的任務集合在任務執行中被更改,可能會致使這個方法的返回值可能會變成未定義的。因此任務列表在提交到這個方法時不要作任何的修改!
Type Parameters: T - the type of the values returned from the tasks
Parameters: tasks - 任務列表
Returns: 返回任務中的其中之一
Throws: InterruptedException - 若是等待時被中斷,這回致使未執行的任務會被取消
NullPointerException - 若是入參任務集合中的某個元素是null
RejectedExecutionException - 如何任意的任務沒法被執行 TimeoutException - 若是超時時間到了,可是沒有任何一個任務完成 ExecutionException - 若是沒有任務執行成功
IllegalArgumentException - 若是集合爲空
執行給定的任務,返回成功完成的某一個任務的結果,任意一個在超時時間以前完成的均可以。在接近返回時(包括正常返回和異常返回),那些沒有完成的任務會被取消。若是給定的任務集合在任務執行中被更改,可能會致使這個方法的返回值可能會變成未定義的。因此任務列表在提交到這個方法時不要作任何的修改!
Type Parameters:
T - the type of the values returned from the tasks
Parameters:
tasks - 任務列表
timeout - 超時時間
unit - 時間單位
Returns:
返回任務中的其中之一
Throws: InterruptedException - 若是等待時被中斷,這回致使未執行的任務會被取消
NullPointerException - 若是入參任務集合中的某個元素是null
RejectedExecutionException - 如何任意的任務沒法被執行 TimeoutException - 若是超時時間到了,可是沒有任何一個任務完成 ExecutionException - 若是沒有任務執行成功
若是此executor已經被關閉,那麼就返回true
Returns: 若是此executor已經被關閉,那麼就返回true
若是關閉以前全部任務都已完成就返回true。這個方法在調用shutdown或shutdownNow以前永遠都不會返回true。
Returns:
若是關閉以前全部任務都已完成就返回true。
調用此方法後,以前提交的任務會被執行,可是任何新任務都不會被執行。當Executor已是關閉狀態後再調用此方法不會任何附加的效果。這個方法不會等待歷史提交的任務去完成執行。若是要那樣請用awaitTermination。(這個描述不是很明朗,須要寫測試類驗證)
Throws:
SecurityException - if a security manager exists and shutting down this ExecutorService may manipulate threads that the caller is not permitted to modify because it does not hold RuntimePermission("modifyThread"), or the security manager's checkAccess method denies access.
嘗試關閉全部激活的任務,當即中止那些正在等待中的任務,而後返回一個包含全部等待執行的任務列表。這個方法不會等待執行中的任務直到執行完畢,若是要那樣請用awaitTermination。
Returns: 還沒來得及執行的任務列表
Throws:
SecurityException - if a security manager exists and shutting down this ExecutorService may manipulate threads that the caller is not permitted to modify because it does not hold RuntimePermission("modifyThread"), or the security manager's checkAccess method denies access.
提交一個帶返回值的任務(Callable)並且返回一個future對象來表明任務結果。若是任務執行成功,future.get方法會返回結果。 下面的代碼可讓你當即阻塞在等待任務完成的階段:
result = exec.submit(aCallable).get();
下面的翻譯還有待學習,學習完後會補充
Note: The Executors class includes a set of methods that can convert some other common closure-like objects, for example, PrivilegedAction to Callable form so they can be submitted.
Type Parameters:
T - the type of the task's result
Parameters: task - 等待執行的任務
Returns: 任務結果future
Throws: RejectedExecutionException - 若是任務沒法被執行 NullPointerException - 若是參數爲null
提交一個Runnable任務,這個任務沒有返回值,可是ExecutorService給它封裝了一個Future,這個Future的get方法在任務執行成功後會返回一個null,也就是說,當你get到null的時候表明任務執行成功了。
Parameters:
task - 任務
Returns:
任務結果future對象
Throws:
RejectedExecutionException - 任務沒法被接受 NullPointerException - 參數爲空
提交一個Runnable任務,這個任務沒有返回值,可是ExecutorService給它封裝了一個Future,這個Future的get方法在任務執行成功後會返回你傳進來的result,也就是說,當你get到result的時候表明任務執行成功了。
Type Parameters:
T - the type of the result
Parameters:
task - 任務
result - 任務結果,在任務完成時future能夠get到
Returns:
任務結果future對象
Throws: RejectedExecutionException - 任務沒法被接受 NullPointerException - 參數爲空