title: Java: How can I notify a thread only if it is waiting?
body: <p>In a first Java thread I have:</p>
  
  <pre><code>while (!isDone) {
      try {
          synchronized (this) {
              wait();
          } 
      } catch (InterruptedException e) {
              e.printStackTrace();
      }
      doSomeVeryLongRunningTask();
  }
  </code></pre>
  
  <p>In another thread, I want to send a notify signal:</p>
  
  <pre><code>synchronized (thr1) {
      thr1.notify();
  }
  </code></pre>
  
  <p>However, if the <code>doSomeVeryLongRunningTask()</code> method is running, I don't want the second thread to get blocked.  I only want to notify the first thread if it is waiting so that the second thread can continue it tasks without getting locked.</p>
  
  <p>How might I fix the code above to accomplish this?</p>