RedSpider1 / concurrent

这是RedSpider社区成员原创与维护的Java多线程系列文章。

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

19章关于stream的并行计算

stayNightL opened this issue · comments

commented

希望能注明stream并行计算的一个坑:并行计算任务共享一个线程池,并且不能方便的修改线程池的线程数量(最起码我没找到,添加启动参数的方法在新版本中已经失效),如果随意的使用并行特性可能会导致方法的吞吐量下降。

感谢反馈,我们把这个注意事项加上去。
自定义线程数量可以这样:

ForkJoinPool customThreadPool = new ForkJoinPool(4);
long actualTotal = customThreadPool
  .submit(() -> roster.parallelStream().reduce(0, Integer::sum)).get();