为了更好地应对大量线程任务结果处理的问题,JDK提供了功能强大的 CompletionService。CompletionService是一个接口,使用创建时提供的 Executor 对象(通常是线程池)来执行任务,并在内部维护了一个阻塞队列(QueueingFuture),当任务执行结束就把任务的执行结果的 Future 对象加入到阻塞队列中。
该接口只有一个实现类: ExecutorCompletionService。
首先看一下 ExecutorCompletionService 的构造函数:
/**
* Creates an ExecutorCompletionService using the supplied
* executor for base task execution and a
* {@link LinkedBlockingQueue} as a completion queue.
*
* @param executor the executor to use
* @throws NullPointerException if executor is {@code null}
*/
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>>();
}
/**
* Creates an ExecutorCompletionService using the supplied
* executor for base task execution and the supplied queue as its
* completion queue.
*
* @param executor the executor to use
* @param completionQueue the queue to use as the completion queue
* normally one dedicated for use by this service. This
* queue is treated as unbounded -- failed attempted
* {@code Queue.add} operations for completed tasks cause
* them not to be retrievable.
* @throws NullPointerException if executor or completionQueue are {@code null}
*/
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;
}
这两个构造方法都需要传入一个线程池,如果不指定 completionQueue,那么默认会使用无界的 LinkedBlockingQueue。任务执行结果的 Future 对象就是加入到 completionQueue 中。
CompletionService 接口提供的方法有 5 个:
public interface CompletionService<V> {
//提交线程任务
Future<V> submit(Callable<V> task);
//提交线程任务
Future<V> submit(Runnable task, V result);
//阻塞等待
Future<V> take() throws InterruptedException;
//非阻塞等待
Future<V> poll();
//带时间的非阻塞等待
Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException;
}
方法简介如下:
接下来,我们重点看一下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;
}
从submit 方法的源码中可以确认两点:
继续追查 QueueingFuture,可以发现:
该类重写了 FutureTask 的done方法,当计算完成时,把Executor执行的计算结果放入BlockingQueue中,而放入结果是按任务完成顺序来进行的,即先完成的任务先放入阻塞队列。
/**
* FutureTask extension to enqueue upon completion
*/
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;
}
由此,CompletionService 实现了生产者提交任务和消费者获取结果的解耦,任务的完成顺序由 CompletionService 来保证,消费者一定是按照任务完成的先后顺序来获取执行结果。
下面我们使用一个小例子,领略一下 CompletionService 的便利:
import java.util.Date;
import java.util.concurrent.*;
/**
* @author guozhengMu
* @version 1.0
* @date 2019/11/8 20:25
* @description
* @modify
*/
public class CompletionServiceTest {
public static void main(String[] args) {
ExecutorService executor = Executors.newCachedThreadPool();
CompletionService<String> cs = new ExecutorCompletionService<>(executor);
// 此线程池运行5个线程
for (int i = 0; i < 5; i++) {
final int index = i;
cs.submit(() -> {
String name = Thread.currentThread().getName();
System.out.println(name + " 启动:" + new Date());
TimeUnit.SECONDS.sleep(10 - index * 2);
return name;
});
}
executor.shutdown();
for (int i = 0; i < 5; i++) {
try {
System.out.println(cs.take().get() + " 结果:" + new Date());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
运行结果:
pool-1-thread-2 启动:Sun Nov 10 11:34:13 CST 2019
pool-1-thread-4 启动:Sun Nov 10 11:34:13 CST 2019
pool-1-thread-3 启动:Sun Nov 10 11:34:13 CST 2019
pool-1-thread-5 启动:Sun Nov 10 11:34:13 CST 2019
pool-1-thread-1 启动:Sun Nov 10 11:34:13 CST 2019
pool-1-thread-5 结果:Sun Nov 10 11:34:15 CST 2019
pool-1-thread-4 结果:Sun Nov 10 11:34:17 CST 2019
pool-1-thread-3 结果:Sun Nov 10 11:34:19 CST 2019
pool-1-thread-2 结果:Sun Nov 10 11:34:21 CST 2019
pool-1-thread-1 结果:Sun Nov 10 11:34:23 CST 2019
通过观察运行结果,可以看到,结果输出的顺序与线程任务执行完成的顺序一致。
package java.util.concurrent;
public class ExecutorCompletionService<V> implements CompletionService<V> {
// 线程池
private final Executor executor;
private final AbstractExecutorService aes;
// 阻塞队列:存放线程执行结果
private final BlockingQueue<Future<V>> completionQueue;
//内部封装的一个用来执线程的FutureTask
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;
}
private RunnableFuture<V> newTaskFor(Callable<V> task) {
if (aes == null)
return new FutureTask<V>(task);
else
return aes.newTaskFor(task);
}
private RunnableFuture<V> newTaskFor(Runnable task, V result) {
if (aes == null)
return new FutureTask<V>(task, result);
else
return aes.newTaskFor(task, result);
}
//构造函数,这里一般传入一个线程池对象executor的实现类
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;
}
//提交线程任务,其实最终还是executor去提交
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;
}
//提交线程任务,其实最终还是executor去提交
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;
}
public Future<V> take() throws InterruptedException {
return completionQueue.take();
}
public Future<V> poll() {
return completionQueue.poll();
}
public Future<V> poll(long timeout, TimeUnit unit) throws InterruptedException {
return completionQueue.poll(timeout, unit);
}
}