public interface ExecutorService extends Executor
Executor ,提供方法来管理终端和方法,可以产生Future为跟踪一个或多个异步任务执行。
一个ExecutorService可以关闭,这将导致它拒绝新的任务。 提供了两种不同的方法来关闭ExecutorService 。 shutdown()方法将允许先前提交的任务在终止之前执行,而shutdownNow()方法可以防止等待任务启动并尝试停止当前正在执行的任务。 一旦终止,执行者没有任务正在执行,没有任务正在等待执行,并且不能提交新的任务。 应关闭未使用的ExecutorService以允许资源的回收。
方法submit延伸的基方法Executor.execute(Runnable)通过创建并返回一个Future可用于取消执行和/或等待完成。 方法invokeAny和invokeAll执行invokeAll执行最常用的形式,执行任务集合,然后等待至少一个或全部完成。 (类别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两个阶段关闭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(); } }
内存一致性效果:操作在提交之前的螺纹Runnable或Callable任务到ExecutorService happen-before由任务采取的任何行动,这反过来又发生-之前结果通过检索Future.get() 。
| Modifier and Type | Method and Description |
|---|---|
boolean |
awaitTermination(long timeout, TimeUnit unit)
阻止所有任务在关闭请求完成后执行,或发生超时,或当前线程中断,以先到者为准。
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks)
执行给定的任务,返回持有他们的状态和结果的所有完成的期货列表。
|
<T> List<Future<T>> |
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
执行给定的任务,返回在所有完成或超时到期时持有其状态和结果的期货列表,以先发生者为准。
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks)
执行给定的任务,返回一个成功完成的结果(即没有抛出异常),如果有的话。
|
<T> T |
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
执行给定的任务,返回一个已经成功完成的结果(即,不抛出异常),如果有的话在给定的超时之前过去。
|
boolean |
isShutdown()
如果此执行者已关闭,则返回
true 。
|
boolean |
isTerminated()
如果所有任务在关闭后完成,则返回
true 。
|
void |
shutdown()
启动有序关闭,其中先前提交的任务将被执行,但不会接受任何新任务。
|
List<Runnable> |
shutdownNow()
尝试停止所有主动执行的任务,停止等待任务的处理,并返回正在等待执行的任务列表。
|
<T> Future<T> |
submit(Callable<T> task)
提交值返回任务以执行,并返回代表任务待处理结果的Future。
|
Future<?> |
submit(Runnable task)
提交一个可运行的任务执行,并返回一个表示该任务的未来。
|
<T> Future<T> |
submit(Runnable task, T result)
提交一个可运行的任务执行,并返回一个表示该任务的未来。
|
void shutdown()
SecurityException - 如果安全管理器存在并关闭,则ExecutorService可能会操纵调用者不允许修改的线程,因为它不会保留RuntimePermission ("modifyThread")或安全管理器的checkAccess方法拒绝访问。
List<Runnable> shutdownNow()
此方法不等待主动执行的任务终止。 使用awaitTermination做到这一点。
除了努力尝试停止处理积极执行任务之外,没有任何保证。 例如,典型的实现将通过Thread.interrupt()取消,所以无法响应中断的任何任务永远不会终止。
SecurityException - 如果安全管理器存在并且关闭此ExecutorService可能会操纵调用者不允许修改的线程,因为它不保留RuntimePermission ("modifyThread") ,或者安全管理器的checkAccess方法拒绝访问。
boolean isShutdown()
true 。
true如果这个执行者已被关闭
boolean isTerminated()
true 。
请注意, isTerminated从不true ,除非shutdown或shutdownNow被称为第一。
true如果所有任务已经完成以后关闭
boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
timeout - 等待的最长时间
unit - 超时参数的时间单位
true如果此执行终止,
false ,如果超时终止前经过
InterruptedException - 如果在等待时中断
<T> Future<T> submit(Callable<T> task)
get方法将在成功完成后返回任务的结果。
如果您想立即阻止等待任务,您可以使用result = exec.submit(aCallable).get();格式的result = exec.submit(aCallable).get();
注意: Executors类包括一组方法,可以将一些其他常见的类似对象的对象,例如PrivilegedAction转换为Callable表单,以便它们可以提交。
T - 任务结果的类型
task - 提交的任务
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果任务为空
<T> Future<T> submit(Runnable task, T result)
get方法将在成功完成后返回给定的结果。
T - 结果的类型
task - 要提交的任务
result - 结果返回
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果任务为空
Future<?> submit(Runnable task)
get方法将返回null 成功完成时。
task - 要提交的任务
RejectedExecutionException - 如果任务无法安排执行
NullPointerException - 如果任务为空
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
Future.isDone()是返回列表的每个元素的true 。
请注意, 完成的任务可能会正常终止或抛出异常。
如果在此操作正在进行中修改了给定的集合,则此方法的结果是未定义的。
T - 从任务返回的值的类型
tasks - 收集任务
InterruptedException - 如果在等待时中断,在这种情况下未完成的任务被取消
NullPointerException - 如果任务或其任何元素是
null
RejectedExecutionException - 如果任何任务无法安排执行
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
Future.isDone()是返回列表的每个元素的true 。
退货后,尚未完成的任务将被取消。
请注意, 完成的任务可能会正常终止或抛出异常。
如果在此操作正在进行中修改了给定的集合,则此方法的结果是未定义的。
T - 从任务返回的值的类型
tasks - 收集任务
timeout - 等待的最长时间
unit - 超时参数的时间单位
InterruptedException - 如果在等待时中断,在这种情况下未完成的任务将被取消
NullPointerException - 如果任务,其任何元素或单位是
null
RejectedExecutionException - 如果任何任务无法安排执行
<T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
T - 从任务返回的值的类型
tasks - 收集任务
InterruptedException - 如果等待中断
NullPointerException - 如果任务或任何要执行的元素任务是
null
IllegalArgumentException - 如果任务为空
ExecutionException - 如果没有任务成功完成
RejectedExecutionException - 如果不能安排执行任务
<T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
T - 从任务返回的值的类型
tasks - 任务的收集
timeout - 等待的最长时间
unit - 超时参数的时间单位
InterruptedException - 如果在等待时中断
NullPointerException - 如果任务,单位或任何可执行的元素任务是
null
TimeoutException - 如果在任务成功完成之前,给定的超时时间过去了
ExecutionException - 如果没有任务成功完成
RejectedExecutionException - if tasks cannot be scheduled for execution
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.