线程在流中 vs 并行流

huangapple go评论58阅读模式
英文:

Thread in stream vs Parallel Stream

问题

我对在性能方面使用哪个不太确定。应该使用哪个?两个版本都具有相同的功能。

代码片段1:

fileMap.values().stream().parallel().forEach(file -> {
    downlod(file);
});

代码片段2:

fileMap.values().forEach(file -> {
    executor.execute(file);
});
英文:

I am unsure about which to use in performance wise. Which should be used? Both version have the some functionality.

Code snippet 1

fileMap.values().stream().parallel().forEach(file -> {
    downlod(file);
});

Code snippet 2

fileMap.values().forEach(file -> {
    executor.execute(file);
}

答案1

得分: 1

在进行大量计算任务时,使用单独的执行器。parallelStream 使用共享线程池,因此繁重的任务可能会阻塞应用程序其他部分中的 parallelStreams

英文:

In case of heavy computation task, go with separate executor. parallelStream uses shared thread pool, thus heavy tasks can potentially block parallelStreams in other parts of application.

huangapple
  • 本文由 发表于 2020年9月22日 18:29:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64007859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定