title: Changing the while loops condition in another thread, nothing happens?
body: <p>So, the other day I made a fractal drawing program, and when it got too slow, I thought I would make it a bit faster by multithreading it. So I introduced a couple of threads to each draw a portion of the image. To know when I was done with the threads, I introduced two variables, <code>AMOUNT_OF_THREADS</code> and <code>threadsDone</code>. I created <code>AMOUNT_OF_THREADS</code> threads, that each drew their own partition and increased <code>threadsDone</code> when done. In the main method, I put in</p>
  
  <pre><code>while (threadsDone &lt; AMOUNT_OF_THREADS)
      ;
  </code></pre>
  
  <p>It surprised me that that loop never finished looping. I knew it because each thread printed out <code>"Thread done"</code> and when the main method loop was done it should have printed out <code>"Main done"</code> but it didn't. I tried to print out <code>threadsDone &lt; AMOUNT_OF_THREADS</code> and now the loop stopped. Weird.</p>
  
  <p>I did some further testing and this problem seems only to occur if the thread takes >2ms to run or the loop takes >2ms each time it loops. Sometimes this bug occurs when the thread takes 2ms to run. </p>
  
  <p>I suspect that the print function made the loop take >2ms to run and that made it work when I printed out the value, but I still wonder why this happens.</p>
  
  <p>So my question is, why does this happen? Is it a bug in the compiler/JVM, or anything else? I asked my dad, and he replied after thinking for a few minutes that it has to be the while loop running too fast, but he wasn't sure.</p>
  
  <p>Here's a little example of some code that has this problem:</p>
  
  <pre><code>public class WhileThread {
  
      public static boolean threadDone = false;
  
      public static void main(String[] args) {
  
          new Thread(new Runnable() {
  
              @Override
              public void run() {
                  try {
                      Thread.sleep(10);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  threadDone = true;
  
                  System.out.println("Thread done.");
              }
          }).start();
  
  
          while (!threadDone) {
  
          }
  
  
          System.out.println("Main program done.");
      }
  
  }
  </code></pre>
  
  <p>When you change the 0 in the <code>Thread.sleep</code> into a 10, this bug never happens.</p>
  
  <p>If it matters, I use a Macbook Air with Java SE 8.</p>