gpt4 book ai didi

java - 在 Java 8 并行流中,终端操作是否在返回之前完成所有计算?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:45:20 25 4
gpt4 key购买 nike

例如,如果类 Foo 有实例方法 bar()

List<Foo> list = new ArrayList<>();
// ...
list.stream()
.parallel()
.forEach(Foo::bar);
doSomething();

是否保证在调用 doSomething() 之前所有对 bar() 的调用都已返回?

最佳答案

是的,除非终端操作是 iterator()spliterator()

来自java.util.stream package page:

In almost all cases, terminal operations are eager, completing their traversal of the data source and processing of the pipeline before returning. Only the terminal operations iterator() and spliterator() are not; these are provided as an "escape hatch" to enable arbitrary client-controlled pipeline traversals in the event that the existing operations are not sufficient to the task.

关于java - 在 Java 8 并行流中,终端操作是否在返回之前完成所有计算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28184913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com