【发布时间】:2020-02-25 21:29:04
【问题描述】:
我有以下代码:
Map<String, Person> targetPerson = targetPersonList
.stream()
.collect(toMap(Person::getKey, Function.identity()));
其中 targetPersonList 是一个相当大的列表,上面的代码大约需要 38 分钟才能完成。 所以我认为下面的代码应该加快一点速度
Map<String, Person> targetPerson = targetPersonList
.parallelStream()
.collect(toMap(Person::getKey, Function.identity()));
实际上正好相反,“平行”的部分,需要 1 小时 20 分钟。我有一个 Core i7 第 8 代,它应该有 6 个核心和 12 个线程,那么有什么问题呢?我对并行流的理解有什么根本错误吗?
【问题讨论】:
-
targetPersonList的运行时类型是什么?它包含多少元素?使用这个简单的管道,并行流的性能更差也就不足为奇了。令人惊讶的是,管道在单线程中运行需要 40 分钟,但在作为并行流运行时,其执行时间仍会增加一倍。
标签: java parallel-processing java-stream