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

Empty while loops do not terminate when condition is satisfied

XMLWordPrintable

    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java -jre7
      java -1.8.0_121
      java -1.8.0_131


      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Professional x64

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      N/A - Tested on multiple windows systems

      A DESCRIPTION OF THE PROBLEM :
      while(!condition) {
       //empty body no code
      }

      Loop does not terminate when condition is satisfied.

      condition = true

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public class Test {

      public static void main(String[] args) throws Exception {
      new Thread(new Runnable() {

      @Override
      public void run() {
      start();
      }

      }).start();

      Thread.sleep(100);
      stopRunning = true;
      System.out.println("But it didn't end?");
      }

      private static boolean stopRunning = false;

      public static void start() {
      while (!stopRunning) {

      }
      System.out.println("Method ended.");
      }

      }


      Please see that System.out.println("Method ended."); does not execute and application will not terminate

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      I was trying to pause the application until a condition was achieved.
      ACTUAL -
      System.out.println("But it didn't end?"); printed however System.out.println("Method ended."); did not. I can see that if you call the method again under System.out.println("But it didn't end?"); the method will terminate however the issue will still be present with the next execution of the method.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      No errors, no crashes.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class Test {

      public static void main(String[] args) throws Exception {
      new Thread(new Runnable() {

      @Override
      public void run() {
      start();
      }

      }).start();

      Thread.sleep(100);
      stopRunning = true;
      System.out.println("But it didn't end?");
      }

      private static boolean stopRunning = false;

      public static void start() {
      while (!stopRunning) {

      }
      System.out.println("Method ended.");
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Not bypass has been found

        1. Test.java
          0.4 kB
        2. TestSynchronized.java
          0.5 kB
        3. TestVolatile.java
          0.4 kB

            zmajo Zoltan Majo (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: