[concurrency-interest] Condition.awaitNanos

David Holmes dcholmes at optusnet.com.au
Fri Mar 10 20:21:51 EST 2006


The behaviour is logically as follows, given a call to awaitNanos(x):

  if (x <= 0) return x; // or any value <=0
  if (interrupted) throw InterruptedException
  long startTime = now();
  if (interrupted) throw InterruptedException
  else if (signalled) return (x-1);
  else return (x - (now() - startTime));

So to answer your questions:

> -what happens if the Condition.awaitNanos is called with a value less
> than or equal to zero? I guess that the function returns immediately
> without any waiting.

Yes it "immediately" returns a value <= 0 (though it might check
interruption first).

> -what happens if the Condition.awaitNanos is called with a value x,
> and the the wait for signalling/interrupt also took time x. So the
> remaining time is 0. 0 indicates that it received a timeout, but zero
> also could be a signal to indicate that the call was succesfull.

The time left need only be checked if there was no signal (the
implementation should know this). So it need only return x-1 to meet the
spec. (That said I think we have a bug in this area with our use of
AbstractQueuedSynchronizer as it might return a negative value after a
signal when a small timeout is used!)

> -what happens if the Condition.awaitNanos is called with a value 1,
> the wait took 0, so the remaining time is 1 (so it should be decreased
> to zero so the value is less than the original timeout).

Ultimately do_block uses some mechanism that will either ignore really small
timeouts, or else cause an immediate return. In either case the thread has
not been signalled so the timeleft is calculated. Doing all this takes far
longer than one nanosecond so you return a negative value that indicates a

Hope that clarifies things.

David Holmes

More information about the Concurrency-interest mailing list