ParallelStreams多线程执行设置线程池数量

// 串行执行流
stream().filter(e -> e > 10).count();
// 并行执行流
parallelStream().filter(e -> e > 10).count()

ParallelStreams 默认使用 ForkJoinPool.commonPool()线程池。

roster.parallelStream().reduce(0, Integer::sum)

 修改线程池大小

ForkJoinPool customThreadPool = new ForkJoinPool(4);
long actualTotal = customThreadPool.submit(() -> roster.parallelStream().reduce(0, Integer::sum)).get();
原文地址:https://www.cnblogs.com/breeze-zZ/p/14915741.html