title: How to use TimeUnit.timedWait() without losing nanosecond precision?
body: <p>I'm trying to implement <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/Future.html#get%28long,%20java.util.concurrent.TimeUnit%29" rel="nofollow noreferrer">Future.get(long, TimeUnit)</a> in terms of <a href="http://java.sun.com/javase/6/docs/api/java/util/concurrent/TimeUnit.html#timedWait%28java.lang.Object,%20long%29" rel="nofollow noreferrer">TimeUnit.timedWait(Object, long)</a>.</p>
  
  <p>It's not clear how to use <code>TimeUnit.timedWait(Object, long)</code> in a way that handles spurious wakeups without losing the nanosecond component of TimeUnit. Normally you'd do something like this:</p>
  
  <pre><code>public V get(long timeout, TimeUnit unit)
  {
    long expirationTime = new Date().getTime() + unit.toMillis(timeout);
    while (!condition)
    {
      long timeLeft = expirationTime - new Date().getTime();
      if (timeLeft &lt;= 0)
        throw new TimeoutException();
      unit.timedWait(object, timeLeft);
    }
  }
  </code></pre>
  
  <p>but you lose the nanosecond component. If everyone is simply dropping the nanosecond component then what is the point of <code>TimeUnit</code> even supporting nanoseconds and offering <code>TimeUnit.timedWait()</code>?</p>