<分区>
我正在尝试理解 reduce 方法。如果我将 reduce 与 stream() 结合使用,我会得到 _ab
,如果我将 reduce 与 parallelStream()
结合使用,我会得到 _a_b
。无论我们使用 parallelStream 还是 stream,reduce 的输出不应该相同吗?
import java.util.*;
import java.util.stream.*;
class TestParallelStream{
public static void main(String args[]){
List<String> l = Arrays.asList("a","b","c","d");
String join=l.stream()
.peek(TestParallelStream::sleepFor)
.reduce("_",(a,b) -> a.concat(b));
System.out.println(join);
}
public static void sleepFor(String w){
System.out.println("inside thread:"+w);
try{
Thread.currentThread().sleep(5000);
}catch(InterruptedException e){ }
}
}