parallelStream和CompletableFuture 默认使用的都是 ForkJoinPool.commonPool() 默认线程池;
基于服务器内核的限制,如果你是八核,每次线程只能起八个,不能自定义线程池;
适用于对list密集计算操作充分利用CPU资源,如果需要调用远端服务不建议使用
public class StreamUtil {
public static final int MAX_SEND = 50;
public static <T> List<List<T>> splitList(List<T> list, int max_send) {
int limit = (int) Math.ceil((double) list.size() / max_send);
return Stream.iterate(0, n -> n + 1).limit(limit).parallel().map(i -> list.stream()
.skip(i * max_send).limit(max_send).parallel().collect(Collectors.toList()))
.collect(Collectors.toList());
}
}
List<CompletableFuture> completableFutures = list.stream().map(fix -> CompletableFuture.runAsync(() -> {
//todo
},fixAssetPool)).collect(Collectors.toList());
completableFutures.forEach(CompletableFuture::join);
@Bean("fixAssetPool")
public ThreadPoolTaskExecutor fixAssetPool(){
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(10);
executor.setMaxPoolSize(40);
executor.setQueueCapacity(100);
executor.setThreadNamePrefix("fixAssetPool-future-");
executor.setRejectedExecutionHandler(new java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy());//设置拒绝策略
return executor;
}
参考:
https://blog.csdn.net/z69183787/article/details/107026179