Java8的CompletionService使用与原理

为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处。LaplaceDemon/ShiJiaqi。

https://www.cnblogs.com/shijiaqi1066/p/10454237.html




CompletionService是Java8的新增接口,JDK为其提供了一个实现类ExecutorCompletionService。这个类是为线程池中Task的执行结果服务的,即为Executor中Task返回Future而服务的。CompletionService的实现目标是任务先完成可优先获取到,即结果按照完成先后顺序排序。

CompletionService的使用非常简单。从源码查看ExecutorCompletionService类,该类只有三个成员变量:

public class ExecutorCompletionService implements CompletionService {
    private final Executor executor;
    private final AbstractExecutorService aes;
    private final BlockingQueue> completionQueue;
    
    ...
}

可以看到ExecutorCompletionService主要是增强executor线程池的。Task包装后被塞入completionQueue,当Task结束,其Future就可以从completionQueue中获取到。

其基本原理可以参看下图:
Java8的CompletionService使用与原理_第1张图片

CompletionService接口源码:

public interface CompletionService {
    // 提交
    Future submit(Callable task);
    Future submit(Runnable task, V result);
    // 获取
    Future take() throws InterruptedException;
    Future poll();
    Future poll(long timeout, TimeUnit unit) throws InterruptedException;
}





例:向CompletionService中提交10个Task,当Task有任务返回则会优先从CompletionService内部的队列中获取到Task的Future。

package test;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class TestCompletionService {
    public static void main(String[] args)  {
        Long start = System.currentTimeMillis();
        //开启3个线程
        ExecutorService exs = Executors.newFixedThreadPool(5);
        try {
            int taskCount = 10;
            // 结果集
            List list = new ArrayList();
            List> futureList = new ArrayList>();
            
            // 1.定义CompletionService
            CompletionService completionService = new ExecutorCompletionService(exs);  
            
            // 2.添加任务
            for(int i=0;i future = completionService.submit(new Task(i+1));
                futureList.add(future);
            }

            // 3.获取结果
            for(int i=0;i

转载于:https://www.cnblogs.com/shijiaqi1066/p/10454237.html

你可能感兴趣的:(java,爬虫)