java8Callable与ThreadPoolExecutor数据分段处理

ThreadPoolExecutor

 public ThreadPoolExecutor(int corePoolSize, // 1
                              int maximumPoolSize,  // 2
                              long keepAliveTime,  // 3
                              TimeUnit unit,  // 4
                              BlockingQueue<Runnable> workQueue, // 5
                              ThreadFactory threadFactory,  // 6
                              RejectedExecutionHandler handler ) { //7
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
    }

一、预定义线程池
1.FixedThreadPool


public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

corePoolSize与maximumPoolSize相等,即其线程全为核心线程,是一个固定大小的线程池,是其优势;
keepAliveTime = 0 该参数默认对核心线程无效,而FixedThreadPool全部为核心线程;
workQueue 为LinkedBlockingQueue(无界阻塞队列),队列最大值为Integer.MAX_VALUE。如果任务提交速度持续大余任务处理速度,会造成队列大量阻塞。因为队列很大,很有可能在拒绝策略前,内存溢出。是其劣势;
FixedThreadPool的任务执行是无序的;
适用场景:可用于Web服务瞬时削峰,但需注意长时间持续高峰情况造成的队列阻塞。

2.CachedThreadPool

public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

corePoolSize = 0,maximumPoolSize = Integer.MAX_VALUE,即线程数量几乎无限制;
keepAliveTime = 60s,线程空闲60s后自动结束。
workQueue 为 SynchronousQueue 同步队列,这个队列类似于一个接力棒,入队出队必须同时传递,因为CachedThreadPool线程创建无限制,不会有队列等待,所以使用SynchronousQueue;
适用场景:快速处理大量耗时较短的任务,如Netty的NIO接受请求时,可使用CachedThreadPool。

3.SingleThreadExecutor

public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

咋一瞅,不就是newFixedThreadPool(1)吗?定眼一看,这里多了一层FinalizableDelegatedExecutorService包装,这一层有什么用呢,写个dome来解释一下:

public static void main(String[] args) {
        ExecutorService fixedExecutorService = Executors.newFixedThreadPool(1);
        ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) fixedExecutorService;
        System.out.println(threadPoolExecutor.getMaximumPoolSize());
        threadPoolExecutor.setCorePoolSize(8);
    ExecutorService singleExecutorService = Executors.newSingleThreadExecutor();

// 运行时异常 java.lang.ClassCastException
// ThreadPoolExecutor threadPoolExecutor2 = (ThreadPoolExecutor) singleExecutorService;
}

对比可以看出,FixedThreadPool可以向下转型为ThreadPoolExecutor,并对其线程池进行配置,而SingleThreadExecutor被包装后,无法成功向下转型。因此,SingleThreadExecutor被定以后,无法修改,做到了真正的Single。

4.ScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

newScheduledThreadPool调用的是ScheduledThreadPoolExecutor的构造方法,而ScheduledThreadPoolExecutor继承了ThreadPoolExecutor,构造是还是调用了其父类的构造方法。

public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
              new DelayedWorkQueue());
    }
    

二、Callable
Callable


public interface Callable<V> {
    V call();
}

Callable接口是jdk1.5新增的接口. 在此之前如果想得到其他线程的一个计算结果通常需要使用共享变量或其他线程通信方式, 而Callable可以理解为一个带返回值的多线程接口. 返回值类型由泛型V决定
和Runnable一样, 接口本身是什么都干不了的; 把一个Runnable对象转换为实际线程的类是Thread, 而负责Callable的类是FutureTask.

Future
public interface Future
Future这个接口的大概意义是这样的——对于某个同步的计算过程, 我们不用关心它的进度; 当我们想拿到结果的时候, 如果计算完成则直接返回结果, 否则就等待那个线程完成计算.
但是为什么命名为"Future"呢…感觉上面这段的说明还不够…

Future声明的方法
//取消任务, 参数表示是否触发线程中断, 即interrupt方法
boolean cancel(boolean)
//是否被取消
boolean isCanceled()
//是否不再运行, 包括运行结束和中途取消等情况
boolean isDone()

//获得运行结果, 如果没有运行完则等待
V get()
//在一段时间内获得运行结果, 如果超时抛出TimeoutException异常
V get(long, timeUnit)
Future接口的实现
在jdk中有2个类实现了该接口, 分别是FutureTask和CompletableFuture. 后者有点麻烦也许以后有机会会提到吧.

FutureTask
public class FutureTask implements RunnableFuture
其中

public interface RunnableFuture extends Runnable, Future
RunnableFuture没有声明新的方法, 略去说明
三,测试代码

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 方法名:          CallableTest
 * 方法功能描述:
 *
 * @param:
 * @return:
 * @Author: 
 * @Create Date:  
 */
public class CallableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        int corePoolSize = 2;
        int maximumPoolSize = 10;
        long keepAliveTime = 10;
        TimeUnit unit = TimeUnit.SECONDS;
        BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(2);
        ThreadFactory threadFactory = new NameTreadFactory();
        RejectedExecutionHandler handler = new MyIgnorePolicy();


        List<Integer> list = getList();

        // 每500条数据开启一条线程
        int threadSize = 500;
        // 总数据条数
        int dataSize = list.size();
        // 线程数
        int threadNum = dataSize / threadSize + 1;
        // 定义标记,过滤threadNum为整数
        boolean special = dataSize % threadSize == 0;

        ThreadPoolExecutor executor = new ThreadPoolExecutor(threadNum, maximumPoolSize, keepAliveTime, unit,
                workQueue, threadFactory, handler);
        executor.prestartAllCoreThreads(); // 预启动所有核心线程

        // 定义一个任务集合
        List<Integer> cutList = null;
        List<Future<HashMap<Integer,Integer>>> futures = new ArrayList<>(5);
        // 确定每条线程的数据
        for (int i = 0; i < threadNum; i++) {
            if (i == threadNum - 1) {
                if (special) {
                    break;
                }
                cutList = list.subList(threadSize * i, dataSize);
            } else {
                cutList = list.subList(threadSize * i, threadSize * (i + 1));
            }
            futures.add(executor.submit(new Task(cutList)));
        }
        for (int i = 0; i < futures.size(); i++) {
            Future<HashMap<Integer, Integer>> hashMapFuture = futures.get(i);
            HashMap<Integer, Integer> map = hashMapFuture.get();
            map.forEach((key,value)->{
                System.out.println(key);
            });
        }
    }

    private static class Task implements Callable<HashMap<Integer,Integer>> {
        private List<Integer> list;

        public Task(List<Integer> list) {
            this.list = list;
        }

        @Override
        public HashMap<Integer,Integer> call() {
            HashMap<Integer, Integer> map = new HashMap<>();
            //返回处理结果
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i) % 19 == 0){
                    map.put(list.get(i),9527);
                }
            }
            return map;
        }
    }

    static class NameTreadFactory implements ThreadFactory {

        private final AtomicInteger mThreadNum = new AtomicInteger(1);

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(r, "my-thread-" + mThreadNum.getAndIncrement());
            System.out.println(t.getName() + " has been created");
            return t;
        }
    }

    public static class MyIgnorePolicy implements RejectedExecutionHandler {

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            doLog(r, e);
        }

        private void doLog(Runnable r, ThreadPoolExecutor e) {
            // 可做日志记录等
            System.err.println( r.toString() + " rejected");
//          System.out.println("completedTaskCount: " + e.getCompletedTaskCount());
        }
    }
    
    private static List<Integer> getList(){
        ArrayList<Integer> list = new ArrayList<>();
        for (int i = 0; i < 3000; i++) {
            list.add(i);
        }
        return list;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值