title: Java Thread not starting (or taking a long time to start)
body: <p>I've got a simple (or at least I thought it was) java application that does some work in a Thread.</p>
  
  <p>Something like:</p>
  
  <pre><code>public class Task implements Runnable {
  
      public boolean running = false;
  
      public void run() {
          running = true;
          // long running processing
      }
  }
  
  ...
  
  void startTask() {
      Task task = new Task();
      Thread thread = new Thread(task);
      thread.start();
      // I added this thinking the calling thread might terminate before
      // the new thread starts
      while (!task.running) {
          try {
              Thread.sleep(1);
          } catch (InterruptedException ie) {
              // log error
      }
  }
  </code></pre>
  
  <p><code>startTask()</code> above is called in response to a REST request (this is a Spring Boot application). It runs fine on my dev machine (Windows 10, Oracle JDK), and on an Amazon EC2 instance (Amazon Linux, OpenJDK), but not on a Google Compute instance (Ubuntu 16.04, OpenJDK). In this latter case, the worker thread either never starts (<code>task.running</code> is never set to <code>true</code>), or it sometimes starts after 60+ seconds. I am bewildered.</p>
  
  <p>Given the task itself is not very complex (plus the fact that setting the "running" flag is the first thing it does, and this never happens) leads me to think this is some weird JVM/system-related issue, but I really have no clue.</p>
  
  <p>The most frustrating thing is it works <em>sometimes</em> (usually the first time I upload/run it after a rebuild). And has never failed to work on my PC.</p>
  
  <p>edit: I have tried using the Oracle JRE in Ubuntu, but with the same lack of success.</p>
  
  <p>2nd edit: Yep, I made some mistakes writing the sample code here. Fixed.</p>