java 快捷使用线程池CompletableFuture

1.创建线程池工具包
 

package com.clouderp.report.utils;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public final class ThreadPoolUtil {
    
    private final static int corePoolSize = Runtime.getRuntime().availableProcessors();


    static volatile int theadCount = 0;

    /**
     * 通用的线程池。
     */
    public static ThreadPoolExecutor commonPool = new ThreadPoolExecutor(corePoolSize + 1,
            corePoolSize * 2 + 1, 10, TimeUnit.SECONDS, new LinkedBlockingQueue<>(5000)
            , r -> new Thread(r, "common thread " + theadCount++)
    );

    /**
     * 定时任务线程池, 注意定时任务要设置合理的定时时间, 要根据任务的耗时来合理设置。 建议定时任务还是使用spring的定时任务功能
     */
    public static ScheduledThreadPoolExecutor schedulePool = new ScheduledThreadPoolExecutor(corePoolSize + 1,
            r -> new Thread(r, "schedulePool thread " + theadCount++)
    );

    /**
     * 短时间批量任务的执行
     *
     * @param poolSize
     * @param runnables
     */
    public static void createPool(int poolSize, List<Runnable> runnables) {
        if (poolSize < 1) {
            throw new RuntimeException("请设置合理的线程池数量");
        }
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(poolSize,
                poolSize, 0, TimeUnit.SECONDS, new LinkedBlockingQueue<>(runnables.size())
                , r -> new Thread(r, "create new Pool thread " + theadCount++));

        runnables.forEach(runnable -> {
            threadPoolExecutor.execute(runnable);
        });
        //这里只是通知线程池不在接收新任务了,并且所有任务结束后,线程池要关闭
        threadPoolExecutor.shutdown();
    }

}

2.处理任务

//1.异步运行  
CompletableFuture<List<OrderDetailAnalysisDto>> orderDetailAnalysisDtosFuture = CompletableFuture.supplyAsync(() ->
        {
            return dataAnalysisMapper.orderDetailAnalysisList(orderDetailAnalysisParam);
        }, ThreadPoolUtil.commonPool);

        CompletableFuture<List<OrderDetailAnalysisDto>> orderCollectionFuture = CompletableFuture.supplyAsync(() -> {
            return dataAnalysisMapper.findOrderCollectionByDealer(orderDetailAnalysisParam);
        },ThreadPoolUtil.commonPool);
//2.阻塞等待异步运行时间最长的方法
 CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(orderDetailAnalysisDtosFuture,orderCollectionFuture, orderOutStockAmountFuture, orderAfterAmountFuture, orderReturnAmountFuture);
voidCompletableFuture.get();
//3.获取运行返回的数据
List<OrderDetailAnalysisDto> orderDetailAnalysisDtos = orderDetailAnalysisDtosFuture.get();
List<OrderDetailAnalysisDto> orderCollection = orderCollectionFuture.get();

3.如果涉及到异常处理等  可以查询CompletableFuture的其他异步方法
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值