|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
public interface ExecutorService
Executor 提供了管理終止的方法,以及可為追蹤一個或多個非同步任務執行狀況而產生 Future 的方法。
可以關閉 ExecutorService,這將導致其拒絕新任務。提供兩個方法來關閉 ExecutorService。shutdown() 方法在終止前允許執行以前提交的任務,而 shutdownNow() 方法阻止等待任務啟動並試圖停止當前正在執行的任務。在終止時,執行程序沒有任務在執行,也沒有任務在等待執行,並且無法提交新任務。應該關閉未使用的 ExecutorService 以允許回收其資源。
通過創建並返回一個可用於取消執行和/或等待完成的 Future,方法 submit 擴展了基本方法 Executor.execute(java.lang.Runnable)。方法 invokeAny 和 invokeAll 是批量執行的最常用形式,它們執行任務 collection,然後等待至少一個,或全部任務完成(可使用 ExecutorCompletionService 類別來編寫這些方法的自定義變體)。
Executors 類別提供了用於此套件中所提供的執行程序服務的處理器方法。
Executors.newFixedThreadPool(int) 處理器方法:
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(如有必要)取消所有遺留的任務:
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();
}
}
記憶體一致性效果:執行緒中向 ExecutorService 提交 Runnable 或 Callable 任務之前的操作 happen-before 由該任務所提取的所有操作,後者依次 happen-before 通過 Future.get() 獲取的結果。
| 方法摘要 | ||
|---|---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit)
請求關閉、發生逾時或者當前執行緒中斷,無論哪一個首先發生之後,都將導致阻塞,直到所有任務完成執行。 |
|
|
invokeAll(Collection<? extends Callable<T>> tasks)
執行給定的任務,當所有任務完成時,返回保持任務狀態和結果的 Future 列表。 |
|
|
invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
執行給定的任務,當所有任務完成或逾時期滿時(無論哪個首先發生),返回保持任務狀態和結果的 Future 列表。 |
|
|
invokeAny(Collection<? extends Callable<T>> tasks)
執行給定的任務,如果某個任務已成功完成(也就是未拋出異常),則返回其結果。 |
|
|
invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
執行給定的任務,如果在給定的逾時期滿前某個任務已成功完成(也就是未拋出異常),則返回其結果。 |
|
boolean |
isShutdown()
如果此執行程序已關閉,則返回 true。 |
|
boolean |
isTerminated()
如果關閉後所有任務都已完成,則返回 true。 |
|
void |
shutdown()
啟動一次順序關閉,執行以前提交的任務,但不接受新任務。 |
|
List<Runnable> |
shutdownNow()
試圖停止所有正在執行的活動任務,暫停處理正在等待的任務,並返回等待執行的任務列表。 |
|
|
submit(Callable<T> task)
提交一個返回值的任務用於執行,返回一個表示任務的未決結果的 Future。 |
|
Future<?> |
submit(Runnable task)
提交一個 Runnable 任務用於執行,並返回一個表示該任務的 Future。 |
|
|
submit(Runnable task,
T result)
提交一個 Runnable 任務用於執行,並返回一個表示該任務的 Future。 |
|
| 從介面 java.util.concurrent.Executor 繼承的方法 |
|---|
execute |
| 方法詳細資訊 |
|---|
void shutdown()
SecurityException - 如果安全管理器存在並且關閉,此 ExecutorService 可能操作某些不允許調用者修改的執行緒(因為它沒有保持 RuntimePermission("modifyThread")),或者安全管理器的 checkAccess 方法拒絕存取。List<Runnable> shutdownNow()
無法保證能夠停止正在處理的活動執行任務,但是會盡力嘗試。例如,通過 Thread.interrupt() 來取消典型的實作,所以任何任務無法回應中斷都可能永遠無法終止。
SecurityException - 如果安全管理器存在並且關閉,此 ExecutorService 可能操作某些不允許調用者修改的執行緒(因為它沒有保持 RuntimePermission("modifyThread")),或者安全管理器的 checkAccess 方法拒絕存取。boolean isShutdown()
boolean isTerminated()
boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
timeout - 最長等待時間unit - timeout 參數的時間單位
InterruptedException - 如果等待時發生中斷<T> Future<T> submit(Callable<T> task)
如果想立即阻塞任務的等待,則可以使用 result = exec.submit(aCallable).get(); 形式的建構。
註:Executors 類別包括了一組方法,可以轉換某些其他常見的類似於閉套件的物件,例如,將 PrivilegedAction 轉換為 Callable 形式,這樣就可以提交它們了。
task - 要提交的任務
RejectedExecutionException - 如果任務無法安排執行
NullPointerException - 如果該任務為 null
<T> Future<T> submit(Runnable task,
T result)
task - 要提交的任務result - 返回的結果
RejectedExecutionException - 如果任務無法安排執行
NullPointerException - 如果該任務為 nullFuture<?> submit(Runnable task)
task - 要提交的任務
RejectedExecutionException - 如果任務無法安排執行
NullPointerException - 如果該任務為 null
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException
Future.isDone() 為 true。注意,可以正常地或通過拋出異常來終止已完成 任務。如果正在進行此操作時修改了給定的 collection,則此方法的結果是不確定的。
tasks - 任務 collection
InterruptedException - 如果等待時發生中斷,在這種情況下取消尚未完成的任務。
NullPointerException - 如果任務或其任意元素為 null
RejectedExecutionException - 如果所有任務都無法安排執行
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException
Future.isDone() 為 true。一旦返回後,即取消尚未完成的任務。注意,可以正常地或通過拋出異常來終止已完成 任務。如果此操作正在進行時修改了給定的 collection,則此方法的結果是不確定的。
tasks - 任務 collectiontimeout - 最長等待時間unit - timeout 參數的時間單位
InterruptedException - 如果等待時發生中斷,在這種情況下取消尚未完成的任務
NullPointerException - 如果任務或其任意元素或 unit 為 null
RejectedExecutionException - 如果所有任務都無法安排執行
<T> T invokeAny(Collection<? extends Callable<T>> tasks)
throws InterruptedException,
ExecutionException
tasks - 任務 collection
InterruptedException - 如果等待時發生中斷
NullPointerException - 如果任務或其任意元素為 null
IllegalArgumentException - 如果任務為空
ExecutionException - 如果沒有任務成功完成
RejectedExecutionException - 如果任務無法安排執行
<T> T invokeAny(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException,
ExecutionException,
TimeoutException
tasks - 任務 collectiontimeout - 最長等待時間unit - timeout 參數的時間單位
InterruptedException - 如果等待時發生中斷
NullPointerException - 如果任務或其任意元素或 unit 為 null
TimeoutException - 如果在所有任務成功完成之前給定的逾時期滿
ExecutionException - 如果沒有任務成功完成
RejectedExecutionException - 如果任務無法安排執行
|
JavaTM 2 Platform Standard Ed. 6 |
|||||||||
| 上一個類別 下一個類別 | 框架 無框架 | |||||||||
| 摘要: 巢狀 | 欄位 | 建構子 | 方法 | 詳細資訊: 欄位 | 建構子 | 方法 | |||||||||
版權所有 2008 Sun Microsystems, Inc. 保留所有權利。請遵守GNU General Public License, version 2 only。