Reduce in parallel?
Paul Sandoz
paul.sandoz at oracle.com
Tue Mar 26 04:29:49 PDT 2013
On Mar 26, 2013, at 12:17 AM, Henry Jen <henry.jen at oracle.com> wrote:
> That's expected, minus is not even associative.
>
Right:
(0 - 1) - 2 != 0 - (1 - 2)
When evaluating a parallel stream the implementation will group elements, operate concurrently on those groups to produce intermediate results, and then operate on the intermediate results in the same manner, repeat until one result remains.
We are slowly improving the JavaDoc with regards to such constraints.
Paul.
> 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