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的其他异步方法