为什么Combiner不影响输出?

Has*_*hra 2 java java-8 java-stream

以下代码输出始终为24.

public static void main(String[] args) throws InterruptedException {
    List<String> list = new ArrayList<String>();
    list.add("java");
    list.add("php");
    list.add("python");
    list.add("perl");
    list.add("c");
    list.add("lisp");
    list.add("c#");
    int s = list.stream().reduce(0, (x, y) -> x + y.length(), (x, y) -> 0);
    System.out.println(s);
    s = list.stream().reduce(0, (x, y) -> x + y.length(), (x, y) -> x - y);
    System.out.println(s);
    s = list.stream().reduce(0, (x, y) -> x + y.length(), (x, y) -> x * y);
    System.out.println(s);

}   
Run Code Online (Sandbox Code Playgroud)

问题是组合器影响我的代码的原因.

Eug*_*ene 5

组合器用于并行流.

但即使添加,代码也可能存在其他问题parallel.他们都违反了一些规则......具体来说:

另外,组合器功能必须与累加器功能兼容; 对于所有你和你,以下必须保持

 combiner.apply(u, accumulator.apply(identity, t)) == accumulator.apply(u, t)
Run Code Online (Sandbox Code Playgroud)

你的合成器违反了这一点,所以根据你拥有的CPU数量 - 你会得到不同的结果 - 这显然是错误的.