`
Donald_Draper
  • 浏览: 984395 次
社区版块
存档分类
最新评论

ExecutorCompletionService解析

    博客分类:
  • JUC
阅读更多
Executor接口的定义:http://donald-draper.iteye.com/blog/2365625
ExecutorService接口定义:http://donald-draper.iteye.com/blog/2365738
Future接口定义:http://donald-draper.iteye.com/blog/2365798
FutureTask解析:http://donald-draper.iteye.com/blog/2365980
CompletionService接口定义:http://donald-draper.iteye.com/blog/2366239

package java.util.concurrent;

/**
 * A {@link CompletionService} that uses a supplied {@link Executor}
 * to execute tasks.  This class arranges that submitted tasks are,
 * upon completion, placed on a queue accessible using {@code take}.
 * The class is lightweight enough to be suitable for transient use
 * when processing groups of tasks.
 *
 ExecutorCompletionService是执行器的补充或者说是辅助,用于执行任务。
 任务提交到ExecutorCompletionService执行后,如果任务执行完,则添加到完成
 队列,我们可以通过take,取回完成任务的结果。ExecutorCompletionService
 是处理集合任务的一个轻量级的实现。

 * <p>
 *
 * <b>Usage Examples.</b>
 *
 实例:
 * Suppose you have a set of solvers for a certain problem, each
 * returning a value of some type {@code Result}, and would like to
 * run them concurrently, processing the results of each of them that
 * return a non-null value, in some method {@code use(Result r)}. You
 * could write this as:
 *
假设将一个确定的问题,分成n个部分,这n个部分可以并发执行,每个部分返回一个非null,结果
在其他一些方法中,使用这些结果。
 * <pre> {@code
 * 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);
 *     }
 * }}</pre>
 *
 * 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:
 *
 假设我们仅想用任务集合中第一个完成任务的结果,并忽略其他任务遇到的异常,
 当第一个完成任务的结果可用时,取消其他任务。
 * <pre> {@code
 * 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);
 * }}</pre>
 */
public class ExecutorCompletionService<V> implements CompletionService<V> {
    private final Executor executor;//执行器
    private final AbstractExecutorService aes;//执行器服务
    private final BlockingQueue<Future<V>> completionQueue;//任务完成队列

    /**
     * FutureTask extension to enqueue upon completion
     扩展FutureTask的队列完成任务
     */
    private class QueueingFuture extends FutureTask<Void> {
        QueueingFuture(RunnableFuture<V> task) {
            super(task, null);
            this.task = task;
        }
	//这个是关键,在FutureTask那篇文章中,我们有讲,及当任务完成时调用done方法,
	//done方法为抽象方法,待子类扩展
        protected void done() { 
	        //当任务任务执行结束时,添加到完成队列
		completionQueue.add(task); 
	}
        private final Future<V> task;
    }
    //根据Callable,创建
    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);
    }
    /**
     * Creates an ExecutorCompletionService using the supplied
     * executor for base task execution and a
     * {@link LinkedBlockingQueue} as a completion queue.
     *
     根据执行器构建ExecutorCompletionService,完成队列默认为LinkedBlockingQueue
     * @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;
	//将完成任务放在LinkedBlockingQueue中
        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.
     *
     根据执行器和完成队列构建ExecutorCompletionService
     * @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 taskes 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;
    }
   //提交执行Callable任务
    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;
    }
   //提交执行Runnable任务
    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;
    }
    //从完成队列take完成任务的结果,没有则阻塞,直到有任务完成
    public Future<V> take() throws InterruptedException {
        return completionQueue.take();
    }
    //从完成队列获取完成任务的结果,没有则返回null
    public Future<V> poll() {
        return completionQueue.poll();
    }
    //从完成队列获取完成任务的结果,没有则超时等待,如果超时等待期间还没有完成任务,返回为null
    public Future<V> poll(long timeout, TimeUnit unit)
            throws InterruptedException {
        return completionQueue.poll(timeout, unit);
    }
}

总结:
ExecutorCompletionService内部关联一个执行器AbstractExecutorService和
一个阻塞的任务完成队列,默认为LinkedBlockingQueue。当提交任务,则包装成QueueingFuture,QueueingFuture
扩展了FutureTask,重写done方法,即在任务执行结束时,添加任务执行结果到完成队列。
而take,poll,超时poll直接委托给完成队列。
0
0
分享到:
评论

相关推荐

    32 请按到场顺序发言—Completion Service详解.pdf

    在使用`ExecutorCompletionService`时,我们需要创建一个`ExecutorService`实例,然后将这个`ExecutorService`传递给`ExecutorCompletionService`的构造函数。接着,我们可以向`ExecutorCompletionService`提交任务...

    Callable,Future的使用方式

    Callable,Future的使用方式,里面使用了三种使用方式分别是FutureTask,ExecutorService,ExecutorCompletionService

    JAVA课程学习笔记.doc

    本篇学习笔记将深入解析Java线程池的框架、结构、原理以及相关源码,帮助读者全面理解线程池的工作机制。 1. 线程池模块结构 线程池框架分为多层结构,其中包括核心实现类、辅助类和接口等组件。例如,`sun.nio.ch....

    多线程相关代码(新)

    包括阻塞队列、阻塞栈、ExecutorService、Future、ExecutorCompletionService、死锁、join、重入锁、读写锁、多线程抢票、信号量、signal/await、ThreadLocal等的实例。

    java多线程并发

    `ExecutorCompletionService`是`java.util.concurrent`包提供的一个类,它结合了`ExecutorService`和`BlockingQueue`的功能,用于管理和获取已完成的任务结果。 综上所述,Java中的多线程并发机制非常强大,不仅...

    java多线程编程实践

    `ExecutorCompletionService`结合了`ExecutorService`和`BlockingQueue`的功能,主要用于管理和监控异步任务的执行结果。 #### 三、锁机制 在多线程编程中,锁是确保数据完整性和一致性的重要手段。`java.util....

    ExecutorService与CompletionService对比详解.docx

    在示例中,创建了一个ExecutorCompletionService实例,它继承自CompletionService并且使用ExecutorService作为底层的执行器。提交任务的方式与ExecutorService类似,但获取结果时,不再直接从列表中获取Future,而是...

    通过多线程任务处理大批量耗时业务并返回结果

    4. `CompletionService`:可能是`ExecutorCompletionService`,它结合了`ExecutorService`和`BlockingQueue`的功能。我们可以使用`CompletionService.take()`方法获取下一个已完成的任务的结果,而不必等待所有任务...

    Java——JUC

    7. **ExecutorCompletionService** - 一个基于`ExecutorService`的增强版服务,用于管理一组异步任务的执行和结果收集。 8. **ScheduledExecutorService** - 支持定时及周期性任务执行的接口,如`...

    浅谈Java多线程处理中Future的妙用(附源码)

    CompletionService&lt;Object&gt; ecs = new ExecutorCompletionService(executor); for (int i = 0; i ; i++) { final Integer t = data[i]; ecs.submit(new Callable() { public Object call() { try { Thread....

    ThreadDemo.rar

    - `ExecutorCompletionService`:用于管理一组异步任务,等待任务完成并获取结果。 - `ForkJoinPool`和`RecursiveTask`/`RecursiveAction`:基于工作窃取算法的并行计算框架。 8. **线程中断和守护线程**: 使用...

    Java线程池学习资料-全

    `ThreadPoolExecutor`的`submit()`返回`Future`对象,而`ExecutorCompletionService`的`submit()`除了返回`Future`,还支持批量处理结果。 当线程池中的线程抛出异常时,如果使用`submit()`,异常会被捕获并封装在`...

    jvaa面试宝典

    - 并发工具类:Semaphore、CyclicBarrier、CountDownLatch、ExecutorCompletionService等。 - Future和Callable接口:理解异步计算,以及如何获取结果。 通过深入学习这些知识点,Java开发者可以更好地准备面试,...

    java.util.concurrent.uml.pdf

    ExecutorCompletionService类是其实现,它利用线程池执行任务,并帮助开发者获取已经完成的任务结果。 Runnable和Callable是两种任务类型。Runnable是任务的一个简单的执行对象,没有返回值。而Callable接口类似于...

    springboot集成amazon aws s3对象存储sdk(javav2)

    CompletionService&lt;PartETag&gt; completionService = new ExecutorCompletionService(executor); for (int i = 1; i ; i++) { UploadPartResponse response = s3Client.uploadPart(uploadRequestBuilder.part...

    JAVA并发编程实践-线程的关闭与取消-学习笔记

    10. **TrackingExecutor任务跟踪**:为了确保任务的正常结束,可以使用`ExecutorCompletionService`来跟踪任务的完成情况,并在必要时取消未完成的任务。 11. **处理异常的线程终止**:线程异常终止时,需要正确...

    java并发框架源码-notes:记录各种学习笔记(Java、算法、框架、数据库、并发、源码...)

    `ExecutorCompletionService`用于批量处理完成的任务,提高效率。 8. **框架源码分析**: 分析如`Akka`、`Quasar`或`Disruptor`等并发框架的源码,可以深入理解如何在Java中构建高效的并发系统,学习其设计思想和...

    java抢票系统源码-POS:邮政

    5. **异步编程**:Java 8引入了CompletableFuture和ExecutorCompletionService等工具,使得开发者能更高效地处理异步任务,提高系统性能。 6. **Web框架**:为了简化开发,项目可能使用Spring Boot或Struts等Web...

    CoreJava:核心Java

    14. **并发编程**:深入研究并发工具类(如CountDownLatch, CyclicBarrier, Semaphore, ExecutorCompletionService等),以及并发容器(如ConcurrentHashMap, CopyOnWriteArrayList等)。 15. **垃圾回收与内存管理...

Global site tag (gtag.js) - Google Analytics