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

the "while" loop output wrong integer

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      generic

      Description

      ADDITIONAL SYSTEM INFORMATION :
      jdk version: jdk 1.8.231 and jdk 11.0.5 oracle official version.
      os: mac and windows 10 x64.

      A DESCRIPTION OF THE PROBLEM :
      the int variable change in the while loop, then output the result is wrong.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run the example code.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      always output "-1".
      ACTUAL -
      after a lot of loop, output the "/".

      ---------- BEGIN SOURCE ----------
      public class WhileLoopTest {
        public void test1() {
          int i = 8;
          while ((i -= 3) > 0) ;
          System.out.println("i = " + i);

        }

        public static void main(String[] args) {
          WhileLoopTest tst = new WhileLoopTest();
          for (int i = 0; i < 50_000; i++) {
            tst.test1();
          }
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      just disable jit or use the AtomInteger.

        public void test2() {
          AtomicInteger i = new AtomicInteger(8);
          while ((i.addAndGet(-3)) > 0) ;
          System.out.println("i = " + i.intValue());
        }

      FREQUENCY : always


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              fmatte Fairoz Matte
              Reporter:
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: