title: How to understand if wait() returned from timeout or from a notify()?
body: <p>I have a waiting thread:</p>
  
  <pre><code>synchronized(sharedCounter) {
    while(sharedCounter &gt; 0) {
      sharedCounter.wait(60000); //wait at most 1 minute
      /*if it wakens from the end of the timeout, it should break the loop
        or it could potentially restart the timeout*/
    }
  }
  </code></pre>
  
  <p>And a thread that can notify:</p>
  
  <pre><code>synchronized (sharedCounter) {
    if(sharedCounter == 0) 
      sharedCounter.notify(); 
  }
  </code></pre>
  
  <p><strong>How can I distinguish a notify from a timeout?</strong></p>
  
  <p>I could do something like this:</p>
  
  <pre><code>synchronized(sharedCounter) {
    while(sharedCounter &gt; 0) {
      sharedCounter.wait(60000); 
      if(sharedCounter == -1) { //if it was a notify()
        //I could save the fact that it was a notify() here
        break;
      }
      //Otherwirse, assume it was a timeout, save the fact and break
      break;
    }
  }
  
  synchronized (sharedCounter) {
     if(sharedCounter == 0) {
       sharedCounter = -1; //to signal that it comes from a notify()
       sharedCounter.notify(); 
     }
  }
  </code></pre>
  
  <p><strong>The problem is, that a spurious wake up would spoil my design.</strong></p>
  
  <p>How would you handle this problem?</p>