Reduce in parallel?
Marcin Przeradzki
kryzoo.m at gmail.com
Mon Mar 25 15:30:20 PDT 2013
Hello Everybody,
if I run the following small app:
import java.util.Arrays;
import java.util.List;
import java.util.function.BinaryOperator;
class Kolekcje {
public static void main(String[] args) {
List<Integer> liczby = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
System.out.println("Reducer (a,b) -> (a+b)");
compareStreams(liczby, (a, b) -> (a + b));
System.out.println("Reducer (a,b) -> (a-b)");
compareStreams(liczby, (a, b) -> (a - b));
}
private static void compareStreams(List<Integer> liczby,
BinaryOperator<Integer> reducer) {
int sum1 = liczby.stream()
.reduce(0, reducer);
int sum2 = liczby.parallelStream()
.reduce(0, reducer);
System.out.printf("stream: %d\nparallelstream: %d\n", sum1, sum2);
if(sum1 != sum2){
System.err.println("ERROR!!!!!");
}
}
}
I get following result:
Reducer (a,b) -> (a+b)
stream: 28
parallelstream: 28
Reducer (a,b) -> (a-b)
stream: -28
parallelstream: -2
ERROR!!!!!
tested on:
openjdk version "1.8.0-ea"
OpenJDK Runtime Environment (build
1.8.0-ea-lambda-nightly-h3728-20130318-b82-b00)
OpenJDK 64-Bit Server VM (build 25.0-b21, mixed mode)
Best regards
Marcin
More information about the lambda-dev
mailing list