定义一个线程池
/**
* @Description:文件下载线程池管理
* @author: yuguiming
* @date: 2023-05-30
*/
@Slf4j
@Configuration
public class DownloadFileThreadPoolExecutor {
//阻塞队列
private static final int WORK_QUEUE = 200;
//线程空闲后的存活时长
private static final int KEEP_ALIVE_TIME = 30;
//Cpu核数
private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors();
//核心线程数量大小
private static final int CORE_POOL_SIZE = CPU_COUNT;
//线程池最大容纳线程数
private static final int MAX_POOL_SIZE = CPU_COUNT * 2 + 1;
//是否允许核心线程超时
private static final boolean ALLOW_CORE_THREAD_TIME_OUT = true;
@Bean("downloadFileExecutor")
public ThreadPoolTaskExecutor asyncTaskExecutor() {
log.error("cpu核心数量: {}核",CPU_COUNT);
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setThreadNamePrefix("downloadFileThread-");//线程前缀
threadPoolTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);//核心线程数
threadPoolTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);//最大线程数
threadPoolTaskExecutor.setQueueCapacity(WORK_QUEUE);//等待队列
threadPoolTaskExecutor.setKeepAliveSeconds(KEEP_ALIVE_TIME);//线程池维护线程所允许的空闲时间,单位为秒
threadPoolTaskExecutor.setAllowCoreThreadTimeOut(ALLOW_CORE_THREAD_TIME_OUT);//是否允许核心线程超时
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());//缓冲队列满了之后的拒绝策略:由调用线程处理(一般是主线程)
threadPoolTaskExecutor.initialize();
return threadPoolTaskExecutor;
}
}
以下载文件打包为例,使用CompletableFuture实现
@Autowired
@Qualifier("downloadFileExecutor")
private ThreadPoolTaskExecutor downloadFileExecutor;
public Result<String> taskFileMultiThreadDownload(RenderTaskFileDownloadDto dto) {
try {
//下载主渲染vr文件include行引用的文件及文件里引用到的file
List<CompletableFuture<Void>> futureList = new ArrayList<>();
for (String s : includeSet) {
//下载文件任务丢到线程中执行
futureList.add(CompletableFuture.runAsync(()->{downLoadIncludeFile(s,dto,rootTargetDir);},downloadFileExecutor));
}
//主线程等待所有子线程处理完
futureList.forEach(CompletableFuture::join);
//打包资源文件
ZipUtil.zip(rootTargetDir);
}catch (Exception e){
e.printStackTrace();
}
//返回压缩包的路径
return ResultUtils.success(uploadFile+dto.getRenderTaskId()+".zip");
}