Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8086380

Stream.reduce and Stream.parallel.reduce return different results

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u45
    • core-libs

      FULL PRODUCT VERSION :
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Darwin 13.4.0 Darwin Kernel Version 13.4.0: Wed Mar 18 16:20:14 PDT 2015; root:xnu-2422.115.14~1/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      Parallel reduction produces the wrong result. Reducing 100,200,300,400,500 by addition with a seed of 50 should give 1550. In fact, sequential reduction gives this result, but parallel reduction results in 1750.

      This is reduction via java.util.Stream

          T reduce(T identity, BinaryOperator<T> accumulator);

      Failing Unit test with Junit and Hamcrest

      import static org.hamcrest.Matchers.*;
      import static org.junit.Assert.assertThat;

      assertThat(Stream.of(1,2,3,4,5).map(i->i*100).reduce( 50,(acc,next) -> acc+next),equalTo(Stream.of(1,2,3,4,5).map(i->i*100).parallel().reduce( 50,(acc,next) -> acc+next)));

      ADDITIONAL REGRESSION INFORMATION:
      Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Failing Unit test with Junit and Hamcrest

      import static org.hamcrest.Matchers.*;
      import static org.junit.Assert.assertThat;

      assertThat(Stream.of(1,2,3,4,5).map(i->i*100).reduce( 50,(acc,next) -> acc+next),equalTo(Stream.of(1,2,3,4,5).map(i->i*100).parallel().reduce( 50,(acc,next) -> acc+next)));

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      1550 and passing test
      ACTUAL -
      1750 and failing test

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import static org.hamcrest.Matchers.*;
      import static org.junit.Assert.assertThat;

      @Test
      public void testMapReduceSeed2(){
      assertThat(Stream.of(1,2,3,4,5).map(i->i*100).reduce( 50,(acc,next) -> acc+next),equalTo(Stream.of(1,2,3,4,5).map(i->i*100).parallel().reduce( 50,(acc,next) -> acc+next)));

        }
      ---------- END SOURCE ----------

            psandoz Paul Sandoz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: