java.lang.Object
java.util.concurrent.ExecutorCompletionService<V>
- 类型参数:
V- 此服务的任务产生和使用的值的类型
- 所有已实现的接口:
CompletionService<V>
使用提供的
Executor 执行任务的 CompletionService 。此类安排提交的任务在完成后放置在可使用 take 访问的队列中。该类足够轻量,适合在处理任务组时临时使用。
使用示例。假设您有一组针对某个问题的求解器,每个求解器都返回某种类型的值 Result ,并希望同时运行它们,在某种方法 use(Result r) 中处理它们中每个返回非空值的结果。你可以这样写:
void solve(Executor e,
Collection<Callable<Result>> solvers)
throws InterruptedException, ExecutionException {
CompletionService<Result> cs
= new ExecutorCompletionService<>(e);
solvers.forEach(cs::submit);
for (int i = solvers.size(); i > 0; i--) {
Result r = cs.take().get();
if (r != null)
use(r);
}
} 假设您想使用任务集的第一个非空结果,忽略任何遇到异常的结果,并在第一个任务准备就绪时取消所有其他任务:
void solve(Executor e,
Collection<Callable<Result>> solvers)
throws InterruptedException {
CompletionService<Result> cs
= new ExecutorCompletionService<>(e);
int n = solvers.size();
List<Future<Result>> futures = new ArrayList<>(n);
Result result = null;
try {
solvers.forEach(solver -> futures.add(cs.submit(solver)));
for (int i = n; i > 0; i--) {
try {
Result r = cs.take().get();
if (r != null) {
result = r;
break;
}
} catch (ExecutionException ignore) {}
}
} finally {
futures.forEach(future -> future.cancel(true));
}
if (result != null)
use(result);
}
- 自从:
- 1.5
-
构造方法总结
构造方法构造方法描述ExecutorCompletionService(Executor executor) 使用提供的执行程序创建 ExecutorCompletionService 以执行基本任务,并使用LinkedBlockingQueue作为完成队列。ExecutorCompletionService(Executor executor, BlockingQueue<Future<V>> completionQueue) 使用提供的执行器执行基本任务并使用提供的队列作为其完成队列创建 ExecutorCompletionService。 -
方法总结
-
构造方法详细信息
-
ExecutorCompletionService
使用提供的执行程序创建 ExecutorCompletionService 以执行基本任务,并使用LinkedBlockingQueue作为完成队列。- 参数:
executor- 要使用的执行器- 抛出:
NullPointerException- 如果执行者是null
-
ExecutorCompletionService
使用提供的执行器执行基本任务并使用提供的队列作为其完成队列创建 ExecutorCompletionService。- 参数:
executor- 要使用的执行器completionQueue- 用作完成队列的队列通常专供此服务使用。此队列被视为无界——已完成任务的Queue.add操作失败尝试导致它们不可检索。- 抛出:
NullPointerException- 如果 executor 或 completionQueue 是null
-
-
方法详情
-
submit
从接口CompletionService复制的描述提交一个有返回值的任务以供执行,并返回一个代表任务未决结果的 Future。完成后,可以接管或轮询此任务。- 指定者:
submit在接口CompletionService<V>中- 参数:
task- 要提交的任务- 返回:
- 代表任务未决完成的 Future
- 抛出:
RejectedExecutionException- 如果无法安排任务执行NullPointerException- 如果任务为空
-
submit
从接口CompletionService复制的描述提交一个 Runnable 任务以供执行并返回一个代表该任务的 Future。完成后,可以接管或轮询此任务。- 指定者:
submit在接口CompletionService<V>中- 参数:
task- 要提交的任务result- 成功完成后返回的结果- 返回:
-
代表任务未决完成的 Future,其
get()方法将在完成时返回给定的结果值 - 抛出:
RejectedExecutionException- 如果无法安排任务执行NullPointerException- 如果任务为空
-
take
从接口CompletionService复制的描述检索并删除代表下一个已完成任务的 Future,如果还没有任务则等待。- 指定者:
take在接口CompletionService<V>中- 返回:
- Future 代表下一个完成的任务
- 抛出:
InterruptedException- 如果在等待时被打断
-
poll
从接口CompletionService复制的描述检索并删除表示下一个已完成任务的 Future,如果不存在则为null。- 指定者:
poll在接口CompletionService<V>中- 返回:
-
Future 表示下一个完成的任务,如果不存在则为
null
-
poll
从接口CompletionService复制的描述检索并删除表示下一个已完成任务的 Future,必要时等待指定的等待时间(如果尚不存在)。- 指定者:
poll在接口CompletionService<V>中- 参数:
timeout- 放弃前等待多长时间,以unit为单位unit- 一个TimeUnit决定如何解释timeout参数- 返回:
-
表示下一个已完成任务的 Future 或
null如果指定的等待时间在任务出现之前已经过去 - 抛出:
InterruptedException- 如果在等待时被打断
-