【Java多线程-4】CompletionService详解

2020-09-16 09:41:47 浏览数 (2)

CompletionService详解

我们知道,通过 Future 和 FutureTask 可以获得线程任务的执行结果,但它们有一定的缺陷:

  • Future:多个线程任务的执行结果,我们可以通过轮询的方式去获取,但普通轮询会有被阻塞的可能,升级轮询会非常消耗cpu。
  • FutureTask:虽然我们可以调用 done 方法,在线程任务执行结束后立即返回或做其他处理,但对批量线程任务结果的管理方面有所不足。

为了更好地应对大量线程任务结果处理的问题,JDK提供了功能强大的 CompletionService。CompletionService是一个接口,使用创建时提供的 Executor 对象(通常是线程池)来执行任务,并在内部维护了一个阻塞队列(QueueingFuture),当任务执行结束就把任务的执行结果的 Future 对象加入到阻塞队列中。 该接口只有一个实现类: ExecutorCompletionService。

1 CompletionService 解析

1.1 构造方法

首先看一下 ExecutorCompletionService 的构造函数:

代码语言:javascript复制
 	/**
     * 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 中。

1.2 方法

CompletionService 接口提供的方法有 5 个:

代码语言:javascript复制
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(Callable task):提交线程任务,交由 Executor 对象去执行,并将结果放入阻塞队列;
  • take():在阻塞队列中获取并移除一个元素,该方法是阻塞的,即获取不到的话线程会一直阻塞;
  • poll():在阻塞队列中获取并移除一个元素,该方法是非阻塞的,获取不到即返回 null ;
  • poll(long timeout, TimeUnit unit):从阻塞队列中非阻塞地获取并移除一个元素,在设置的超时时间内获取不到即返回 null ;

接下来,我们重点看一下submit 的源码:

代码语言:javascript复制
 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 方法的源码中可以确认两点:

  1. 线程任务确实是由 Executor 对象执行的;
  2. 提交某个任务时,该任务首先将被包装为一个QueueingFuture。

继续追查 QueueingFuture,可以发现: 该类重写了 FutureTask 的done方法,当计算完成时,把Executor执行的计算结果放入BlockingQueue中,而放入结果是按任务完成顺序来进行的,即先完成的任务先放入阻塞队列。

代码语言:javascript复制
 /** 
  * 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 来保证,消费者一定是按照任务完成的先后顺序来获取执行结果。

2 CompletionService 使用示例

下面我们使用一个小例子,领略一下 CompletionService 的便利:

代码语言:javascript复制
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();
            }
        }
    }
}

运行结果:

代码语言:javascript复制
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

通过观察运行结果,可以看到,结果输出的顺序与线程任务执行完成的顺序一致。

3 完整源码

代码语言:javascript复制
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);
    }
}

0 人点赞