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

Integer.MAX_VALUE breaks while loop

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 6u26
    • core-libs
    • sparc
    • solaris_10

      FULL PRODUCT VERSION :
      java version "1.6.0_20"
      Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
      Java HotSpot(TM) Server VM (build 16.3-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      SunOS adapp4-apandey2 5.10 Generic_142910-17 i86pc i386 i86pc Solaris

      A DESCRIPTION OF THE PROBLEM :
      Using a counter in a while loop where the loop exit condition is like

      public void run()
      {
      while(true)
      {
            int count = 0;
            while(counter++ < Integer.MAX_VALUE)
            {......}
      }
      }

      This breaks the while loop after a few runs. After a few runs, the while loop exits after the first run itself

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Running the code attached and following the comments will reproduce the issue. After a few runs, count will show 1 always and q.size will keep on increasing because queue reader does not consume all while the writer keeps on adding the messages to the queue.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected result - queue reader consumes all messages in the queue.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      mport java.util.concurrent.Executors;
      import java.util.concurrent.LinkedBlockingQueue;
      import java.util.concurrent.ScheduledExecutorService;
      import java.util.concurrent.TimeUnit;
      import java.util.concurrent.atomic.AtomicInteger;

      /**
       * Created by IntelliJ IDEA.
       * User: archit.pandey
       * Date: Sep 29, 2011
       * Time: 10:23:20 PM
       * Bug Testcase for while loop breaking when Integer.MAX_VALUE is used as a breaking condition from the loop.
       * Using a counter in a while loop where the loop exit condition is like
       *
       * public void run()
       * {
       * while(true)
       * {
       * int count = 0;
       * while(counter++ < Integer.MAX_VALUE)
       * {......}
       * }
       * }
       *
       * This breaks the while loop after a few runs. After a few runs, the while loop exits after the first run itself
       */
      public class TestQBreak {
          //executors for running queue reader and writers
          private static ScheduledExecutorService readerSvc = Executors.newSingleThreadScheduledExecutor();
          private static ScheduledExecutorService writerSvc = Executors.newSingleThreadScheduledExecutor();

          private static void testQ() {
              
              final AtomicInteger totalAddCount = new AtomicInteger(10000);
              final LinkedBlockingQueue q = new LinkedBlockingQueue(10000);
              Thread writer = new Thread() {
                  public void run() {
                      System.out.println("Started writer");
                      try {
                          int count = 0;
                          while (count++ < 2000) {
                              q.add(new Integer(0));

                          }
                          System.out.println("total Add count = " + totalAddCount.addAndGet(count));
                      } catch (Throwable t) {
                          t.printStackTrace();
                          System.out.println("Ending writer");
                      }
                  }
              };
              writerSvc.scheduleAtFixedRate(writer, 0, 2000, TimeUnit.MILLISECONDS);

              Thread reader = new Thread() {
                  public void run() {
                      System.out.println("Started reader");
                      try {
                          Integer intE;
                          int count = 0;
                          while (count++ < Integer.MAX_VALUE) { //This is where it breaks after the first run
                              intE = (Integer) q.poll();
                              if (intE == null) {
                                  System.out.println("Queue object nulll");
                                  break;
                              }
                          }
                          //after a few runs - count will show as 1 and q.size will show increasing
                          System.out.println("Reader Got [" + count + "] msgs - [" + q.size() + "]");
                      } catch (Throwable t) {
                          t.printStackTrace();
                          System.out.println("Ending reader");
                      }
                  }
              };
              readerSvc.scheduleAtFixedRate(reader, 0, 1000, TimeUnit.MILLISECONDS);
          }

          public static void main(String[] args) throws Exception {
              testQ();
          }
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      Keep while loop exit condition lesser than Integer.MAX_VALUE

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: