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

Generator called too many times for generated&limited&parallel IntStream(Stream)

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      An parallel and generated IntStream, and set limit to 5, the generator should called 5 times, the consumer(forEach) should processing in multiple threads, but generator called more then 5 times(in java 8, it called hundreds times, and processing in one thread in java 8)

      REGRESSION : Last worked in version 10.0.1

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      copy below source code to a main method:
      AtomicInteger i = new AtomicInteger();
              IntStream
                      .generate(
                              () -> {
                                  int item = i.incrementAndGet();
                                  System.out.println("[generator]: "
                                          + Thread.currentThread().getName()
                                          + ", item:" + item);
                                  return item;
                              })
                      .limit(5)
                      .parallel()
                      .forEach(
                              item -> {
                                  System.out.println("[consumer]: "
                                          + Thread.currentThread().getName()
                                          + ", item:" + item);
                              });

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      [generator]: main, item:1
      [consumer]: main, item:1
      [generator]: ForkJoinPool.commonPool-worker-11, item:2
      [generator]: ForkJoinPool.commonPool-worker-4, item:4
      [generator]: main, item:3
      [generator]: ForkJoinPool.commonPool-worker-9, item:5
      [consumer]: ForkJoinPool.commonPool-worker-4, item:4
      [consumer]: ForkJoinPool.commonPool-worker-11, item:2
      [consumer]: ForkJoinPool.commonPool-worker-13, item:5
      [consumer]: main, item:3
      ACTUAL -
      [generator]: main, item:1
      [generator]: ForkJoinPool.commonPool-worker-4, item:2
      [generator]: ForkJoinPool.commonPool-worker-9, item:3
      [consumer]: main, item:1
      [generator]: ForkJoinPool.commonPool-worker-2, item:6
      [consumer]: ForkJoinPool.commonPool-worker-9, item:3
      [generator]: ForkJoinPool.commonPool-worker-13, item:5
      [generator]: ForkJoinPool.commonPool-worker-11, item:4
      [consumer]: ForkJoinPool.commonPool-worker-4, item:2
      [consumer]: ForkJoinPool.commonPool-worker-13, item:5
      [generator]: ForkJoinPool.commonPool-worker-9, item:10
      [generator]: ForkJoinPool.commonPool-worker-6, item:9
      [generator]: ForkJoinPool.commonPool-worker-15, item:8
      [consumer]: ForkJoinPool.commonPool-worker-2, item:6
      [generator]: main, item:7


      ---------- BEGIN SOURCE ----------
          public static void main(String[] args) {
              AtomicInteger i = new AtomicInteger();
              IntStream
                      .generate(
                              () -> {
                                  int item = i.incrementAndGet();
                                  System.out.println("[generator]: "
                                          + Thread.currentThread().getName()
                                          + ", item:" + item);
                                  return item;
                              })
                      .parallel()
                      .forEach(
                              item -> {
                                  System.out.println("[consumer]: "
                                          + Thread.currentThread().getName()
                                          + ", item:" + item);
                              });
          }
      ---------- END SOURCE ----------

      FREQUENCY : always


        Attachments

        1. JI9053574.java
          0.6 kB
        2. 10_0_1.log
          0.7 kB
        3. 11_ea.log
          0.4 kB
        4. 8u172.log
          26 kB

          Activity

            People

            • Assignee:
              psandoz Paul Sandoz
              Reporter:
              webbuggrp Webbug Group
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: