ExecutorCompletionService
public class ExecutorCompletionService
extends Object
implements CompletionService<V>
CompletionService
使用提供的Executor
执行任务。 本课程安排提交的任务在完成后放置在可使用take
访问的队列中。 该类轻量级足以适用于处理任务组时的瞬时使用。
用法示例。 假设你有一套针对某个问题的求解器,每个都返回一个类型为Result
的值,并且想要并发地运行它们,在一些方法use(Result r)
处理每个返回非空值的use(Result r)
。 你可以这样写:
void solve(Executor e,
Collection<Callable<Result>> solvers)
throws InterruptedException, ExecutionException {
CompletionService<Result> ecs
= new ExecutorCompletionService<Result>(e);
for (Callable<Result> s : solvers)
ecs.submit(s);
int n = solvers.size();
for (int i = 0; i < n; ++i) {
Result r = ecs.take().get();
if (r != null)
use(r);
}
}
Suppose instead that you would like to use the first non-null result of the set of tasks, ignoring any that encounter exceptions, and cancelling all other tasks when the first one is ready:
void solve(Executor e,
Collection<Callable<Result>> solvers)
throws InterruptedException {
CompletionService<Result> ecs
= new ExecutorCompletionService<Result>(e);
int n = solvers.size();
List<Future<Result>> futures = new ArrayList<>(n);
Result result = null;
try {
for (Callable<Result> s : solvers)
futures.add(ecs.submit(s));
for (int i = 0; i < n; ++i) {
try {
Result r = ecs.take().get();
if (r != null) {
result = r;
break;
}
} catch (ExecutionException ignore) {}
}
}
finally {
for (Future<Result> f : futures)
f.cancel(true);
}
if (result != null)
use(result);
}
Summary
Public constructors
ExecutorCompletionService
ExecutorCompletionService (Executor executor)
使用提供的执行器为基础任务执行创建一个ExecutorCompletionService,并将 LinkedBlockingQueue
作为完成队列创建。
Parameters |
executor |
Executor : the executor to use |
ExecutorCompletionService
ExecutorCompletionService (Executor executor,
BlockingQueue<Future<V>> completionQueue)
使用提供的执行器为基础任务执行创建一个ExecutorCompletionService,并将提供的队列作为其完成队列。
Parameters |
executor |
Executor : the executor to use |
completionQueue |
BlockingQueue : the queue to use as the completion queue normally one dedicated for use by this service. This queue is treated as unbounded -- failed attempted Queue.add operations for completed tasks cause them not to be retrievable. |
Public methods
poll
Future<V> poll (long timeout,
TimeUnit unit)
检索并删除表示下一个已完成任务的Future,如果需要,则等待至指定的等待时间(如果没有任何时间存在)。
Parameters |
timeout |
long : how long to wait before giving up, in units of unit |
unit |
TimeUnit : a TimeUnit determining how to interpret the timeout parameter |
Returns |
Future<V> |
the Future representing the next completed task or null if the specified waiting time elapses before one is present |
poll
Future<V> poll ()
检索并删除表示下一个完成的任务的Future,或者如果没有任何任务存在,则表示 null
。
Returns |
Future<V> |
the Future representing the next completed task, or null if none are present |
submit
Future<V> submit (Runnable task,
V result)
提交可执行的任务并返回表示该任务的Future。 完成后,可以采取或调查此任务。
Parameters |
task |
Runnable : the task to submit |
result |
V : the result to return upon successful completion |
Returns |
Future<V> |
a Future representing pending completion of the task, and whose get() method will return the given result value upon completion |
submit
Future<V> submit (Callable<V> task)
提交执行的返回值任务,并返回表示未完成任务结果的Future。 完成后,可以采取或调查此任务。
Parameters |
task |
Callable : the task to submit |
Returns |
Future<V> |
a Future representing pending completion of the task |
take
Future<V> take ()
检索并删除表示下一个已完成任务的Future,如果尚未存在,则等待。
Returns |
Future<V> |
the Future representing the next completed task |