title: Threaded class variable not being set correctly
body: <p>I have a threaded class shown below, which should terminate upon <code>m_bExit</code> being set to false. The boolean <code>m_bMessageReceived</code> is set externally.</p>
  
  <pre><code>public class CommsTimeout extends Thread {
  
      public static boolean m_bMessageReceived = false;
      public volatile boolean m_bExit = false;
      private static long m_nStartTime;
  
      public void run() {
  
          while (!m_bExit) {
              while ((System.currentTimeMillis() &lt; (m_nStartTime + Constants.PERIOD))) {
                  // Wait...
              }
              if (!m_bMessageReceived) {
                  // Do stuff.
              }
  
              m_bMessageReceived = false;
          }
      }
  
      public CommsTimeout() {
  
          // Reset flags.
          m_bMessageReceived = false;
          m_bExit = false;
          m_nStartTime = System.currentTimeMillis();
          this.start();
      }
  }
  </code></pre>
  
  <p>The <code>CommsTimeout</code> class is being initialised in another class...</p>
  
  <pre><code>m_threadCommsTimeout = new CommsTimeout();
  </code></pre>
  
  <p>... and <code>m_bMessageReceived</code> is being set as so in another class every few miliseconds:</p>
  
  <pre><code>CommsTimeout.m_bMessageReceived = true;
  </code></pre>
  
  <p>My problem is that even though <code>m_bMessageReceived</code> is being set to <code>true</code> within the timeout period, the <code>// Do stuff</code> line is still being reached. What have I missed? Thanks!</p>