[concurrency-interest] Condition.awaitNanos

Doug Lea dl at cs.oswego.edu
Fri Mar 10 20:37:00 EST 2006

Peter Veentjer wrote:
> I have a question about the Condition.awaitNanos function. The
> documentation says that if a 0 or less is returned if a timeout has
> occured.
> "A value less than or equal to zero if the wait has timed out; "
> It also says that a value less than the nanosTimeout is returned, when
> it is signalled or interrupted:
> "otherwise an estimate, that is strictly less than the nanosTimeout
> argument, of the time still remaining when this method returned."

Notice that on interrupt, InterruptException is always thrown,
so no value is returned.

Also note that the "otherwise" case return value can also be <= 0.
(which is the basis of two of your other questions.)
While possibly ambiguous, for condition waits, callers will not care since
they will be checking the predicates they are looking for anyway.
The conventions here wouldn't work for something like your tryLockNanos
where you need a return value that tells you both about success
and about remaining time. I think you will need to come up with
some other way to do that.

> -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.


> -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.

Right. (Except not so for interrupts). The intent here is that
zero means "whether or not the predicate you are checking holds,
you need not call awaitNanos again if you are waiting in a loop because
the timeout has now elapsed". This information is what
most timed wait-loops want to know, which is why we return this value.


More information about the Concurrency-interest mailing list