title: Best Method to pause a Thread?
body: <p>I have a Runnable class which performs an operation continuously in its run method using an infinite while loop. Sometimes a user might want to pause/resume the operation. What is the best method to pause the Thread. I have two thoughts:</p>
  
  <p><strong>FIRST</strong></p>
  
  <pre><code>class Foo extends Runnable {
      @Override
      public void run() {
      while(true) {
          if(pauseFlag)
             try{
                Thread.sleep();
             } catch(InterrruptedException ie) {//...}
      }
  }
  </code></pre>
  
  <p><strong>SECOND</strong></p>
  
  <pre><code>class Foo extends Runnable {
      @Override
      public void run() {
          while(true) {
              //pause if user wants to pause
              while(pauseFlag);
          }
      }
  }
  </code></pre>
  
  <ul>
  <li>In the <strong>FIRST</strong> case, when i use Thread.sleep inside loop the Netbeans IDE issues warning not to use Thread.sleep inside loop. Why is that?</li>
  <li>In the <strong>SECOND</strong> case, when i use infinite empty while loop, is that a performance overhead?</li>
  <li>Which method(mentioned above or otherwise) should i prefer to pause the action being performed by the Thread according to the users choice?</li>
  </ul>
  
  <p>Because it has nothing to do with synchronization, i do not want to use wait/notify.</p>