title: Java: is wait(timeout) at least as precise as sleep(timeout) in suspending for timeout millisecs?
body: <p>(<strong>Duplication:</strong> there are a lot of questions about wait() vs sleep(), but I need this specific detail, which I haven't found addressed in any on the answers.)</p>
  
  <p><strong>Context:</strong> </p>
  
  <p>I have a scheduler implemented with <code>sleep()</code>, but I want to change its implementation to use <code>wait(timeout)</code> instead, so to have better control on "waking" it up, using <code>notify()</code> on a precise object instead of <code>interrupt()</code>.</p>
  
  <p>The main advantage is that the content code (which can contain other <code>sleep()</code>s, other <code>wait()</code>s, blocking I/O, etc., in the same thread, possibly from various libraries, etc.) doesn't have to ensure a given management of the interrupt. <strong>Notifying a specific object I'm sure to wake up only that specific <code>wait()</code> with absolutely no impact on other code in that thread.</strong></p>
  
  <p>Using <code>wait(timeout)</code> + <code>notify()</code> seems the best way to ensure not to disrupt any other code in the thread.</p>
  
  <p>Only the scheduler thread and the thread possibly asking to wake up can contend the lock for that specific object, hence obtaining the lock should not be a tangible delay.</p>
  
  <p>As for spurious wake-ups, I presume I can test the actually elapsed time and proceed to wait again if needed. (Which defends against "too little time suspended" but not against "too much time suspended", hence the question.)</p>
  
  <p><strong>Question:</strong></p>
  
  <p><strong>Can I assume that, on any JVM or so, <code>wait(timeout)</code> is at least as precise as <code>sleep(timeout)</code>, in suspending for timeout milliseconds, as precisely as possible?</strong></p>
  
  <p>Also, is the issue with "spurious wake-up" from <code>wait()</code> present also with <code>sleep()</code>?</p>
  
  <p>In other words: to suspend for timeout milliseconds, have I any disadvantages in using a <code>wait(timeout)</code> instead of a <code>sleep(timeout)</code> apart for the following?</p>
  
  <ul>
  <li><p>having to obtaining the lock (presumably negligible in the context)</p></li>
  <li><p>defend against spurious wake-up (assuming <code>sleep()</code> is immune instead, confirm?)</p></li>
  </ul>
  
  <p>Should I expect any degradation in scheduling precision?</p>
  
  <p>Again, please note that the main goal is to wait timeout milliseconds, I'm using <code>wait(timeout)</code> as a <code>sleep()</code> that can be waked up in a more manageable way, without disrupting any other code in that thread.</p>