2
我想了解reduce方法。如果我使用流()減少我得到_ab
,如果我使用減少與parallelStream()
我得到。無論我們使用parallelStream還是stream,reduce的輸出不應該相同嗎?java 8用parallelStream和stream減少
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){ }
}
}