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

Stack overflow error with Consumer interface

    Details

      Description

      FULL PRODUCT VERSION :
      java version "9-ea"
      Java(TM) SE Runtime Environment (build 9-ea+116)
      Java HotSpot(TM) 64-Bit Server VM (build 9-ea+116, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 10.0.10586]

      A DESCRIPTION OF THE PROBLEM :
      The java.util.function.Consumer interface has a utility "andThen" method for chaining Consumers. The implementation calls each Consumer recursively, and a long chain leads to a stack overflow. The other functional interfaces might have similar problems.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Construct a long Consumer chain, by calling the andThen method.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected to see a long printout of dots.
      ACTUAL -
      Exception in thread "main" java.lang.StackOverflowError
              at java.util.function.Consumer.lambda$andThen$0(java.base@9-ea/Consumer.java:65)
              at java.util.function.Consumer.lambda$andThen$0(java.base@9-ea/Consumer.java:65)
              at java.util.function.Consumer.lambda$andThen$0(java.base@9-ea/Consumer.java:65)
      ...

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class ConsumerTest implements Consumer<Object> {
          public static void main(String[] args) throws Exception {
              Consumer<Object> c = new ConsumerTest();
              for (int i=0; i<10000; i++) {
                  c = c.andThen(new ConsumerTest());
              }
              c.accept(null);
          }

          @Override
          public void accept(Object obj) {
              System.out.print('.');
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Define a custom class for chaining Consumers.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Created:
                  Updated:
                  Resolved: