Reduce in parallel?

Henry Jen henry.jen at oracle.com
Mon Mar 25 16:17:13 PDT 2013


That's expected, minus is not even associative.

Cheers,
Henry

On Mar 25, 2013, at 3:30 PM, Marcin Przeradzki <kryzoo.m at gmail.com> wrote:

> 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