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

Parallel Stream not processing whole data provided

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      jdk1.8.0_181

      A DESCRIPTION OF THE PROBLEM :
      We are using parallel stream in our code. Providing it only a limited set of data and putting it into map. After processing, when reading the data using parallel stream, size of the map is not coming correct always.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Create a hashmap
      2. Create a list of values
      3. Process it using parallel stream and put each data read into the map

      Ex code snippet:
      Map<String, String> map = new HashMap<>();
      List<Integer> values = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
      values.parallelStream().forEach(value -> map.put(value.toString(), value.toString()));
      System.out.println(map.size());

      ACTUAL -
      Size is coming as 7 (approx 1 in 30 times)

      ---------- BEGIN SOURCE ----------
      import java.util.Arrays;
      import java.util.HashMap;
      import java.util.List;
      import java.util.Map;

      public class Test {
          
          public static void main(String args[]) {
              Map<String, String> map = new HashMap<>();
              List<Integer> values = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);

              values.parallelStream()
                      .forEach(value -> map.put(value.toString(), value.toString()));
              System.out.println(map.size());
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      We are using stream instead of parallel stream

      FREQUENCY : occasionally


            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: