1.官方文档
A CompletionService that uses a supplied Executor to execute
tasks. This class arranges that submitted tasks are, upon
completion, placed on a queue accessible using take. The class
is lightweight enough to be suitable for transient use when
processing groups of tasks.
Usage Examples. Suppose you have a set of solvers for a
certain problem, each returning a value of some type Result,
and would like to run them concurrently, processing the results
of each of them that return a non-null value, in some method
use(Result r). You could write this as:
一个CompletionService,它使用提供的Executor来执行任务。 该类安排完成时提交的任务放置在可使用take访问的队列中。 该类足够轻巧,适合在处理任务组时进行临时使用。
用法示例。 假设你有一组针对某个问题的solvers,每个都返回某个类型的Result值,并希望并发运行它们,处理每个返回非空值的结果,在某些方法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:
相反,假设您希望使用任务集的第一个非null结果,忽略任何遇到异常,并在第一个任务准备就绪时取消所有其他任务:
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<Future<Result>>(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);
}
2.为什么引入ExecutorCompletionService
控制任务组的传统方式:
List<Callable<T>> tasks = ...;
List<Future<T>> results = executor.invokeAll(tasks);
for (Future<T> result : results) {
processFurther(result.get());
}
缺点是如果前面的任务花了很多时间,则不得不进行等待。
将结果按照可获得的顺序保存起来更有实际意义。可以用ExecutorCompletionService来进行排列。
ExecutorCompletionService<T> service = new ExecutorCompletionService<>(executor);
for (Callable<T> task: tasks) {
service.submit(task);
}
for (int i = 0; i < tasks.size(); i++) {
processFurther(service.take().get());
}
3.源码解析
3.1 构造器
public ExecutorCompletionService(Executor executor) {
if (executor == null)
throw new NullPointerException();
this.executor = executor;
this.aes = (executor instanceof AbstractExecutorService) ?
(AbstractExecutorService) executor : null;
this.completionQueue = new LinkedBlockingQueue<Future<V>>();
}
public ExecutorCompletionService(Executor executor,
BlockingQueue<Future<V>> completionQueue) {
if (executor == null || completionQueue == null)
throw new NullPointerException();
this.executor = executor;
this.aes = (executor instanceof AbstractExecutorService) ?
(AbstractExecutorService) executor : null;
this.completionQueue = completionQueue;
}
3.2 submit
public Future<V> submit(Callable<V> task) {
if (task == null) throw new NullPointerException();
RunnableFuture<V> f = newTaskFor(task);
executor.execute(new QueueingFuture(f));
return f;
}
public Future<V> submit(Runnable task, V result) {
if (task == null) throw new NullPointerException();
RunnableFuture<V> f = newTaskFor(task, result);
executor.execute(new QueueingFuture(f));
return f;
}
private class QueueingFuture extends FutureTask<Void> {
QueueingFuture(RunnableFuture<V> task) {
super(task, null);
this.task = task;
}
protected void done() { completionQueue.add(task); }
private final Future<V> task;
}
newTaskFor一般情况下新建FutureTask对象,所以来看看FutureTask的run方法:
public void run() {
if (state != NEW ||
!UNSAFE.compareAndSwapObject(this, runnerOffset,
null, Thread.currentThread()))
return;
try {
Callable<V> c = callable;
if (c != null && state == NEW) {
V result;
boolean ran;
try {
result = c.call();
ran = true;
} catch (Throwable ex) {
result = null;
ran = false;
setException(ex);
}
if (ran)
set(result);
}
} finally {
// runner must be non-null until state is settled to
// prevent concurrent calls to run()
runner = null;
// state must be re-read after nulling runner to prevent
// leaked interrupts
int s = state;
if (s >= INTERRUPTING)
handlePossibleCancellationInterrupt(s);
}
}
protected void set(V v) {
if (UNSAFE.compareAndSwapInt(this, stateOffset, NEW, COMPLETING)) {
outcome = v;
UNSAFE.putOrderedInt(this, stateOffset, NORMAL); // final state
finishCompletion();
}
}
private void finishCompletion() {
// assert state > COMPLETING;
for (WaitNode q; (q = waiters) != null;) {
if (UNSAFE.compareAndSwapObject(this, waitersOffset, q, null)) {
for (;;) {
Thread t = q.thread;
if (t != null) {
q.thread = null;
LockSupport.unpark(t);
}
WaitNode next = q.next;
if (next == null)
break;
q.next = null; // unlink to help gc
q = next;
}
break;
}
}
done();
callable = null; // to reduce footprint
}
可以看到当任务完成后,会调用钩子方法done,该done方法是在QueueingFuture中定义的,即将完成任务的task放入到阻塞队列中completionQueue。
protected void done() { completionQueue.add(task); }
3.3 take
public Future<V> take() throws InterruptedException {
return completionQueue.take();
}