RFR(s) #2: 6344935: (spec) clarify specifications for Object.wait overloads

David Holmes david.holmes at oracle.com
Fri Aug 25 05:53:12 UTC 2017

On 25/08/2017 3:26 PM, Martin Buchholz wrote:
> On Thu, Aug 24, 2017 at 7:49 PM, David Holmes <david.holmes at oracle.com 
> <mailto:david.holmes at oracle.com>> wrote:
>     I have no further comments on the actual spec.
>     Here's my suggestion for the timed-wait example :)
>     while (true) {
>        if (!condition)
>          wait(timeout, nanos);
>        else
>          break; // condition holds
>        <recompute timeout>
>        if (timeout <= 0 && !condition)
>          throw new TimedOutException();
>     }
>     // Perform action appropriate to condition
> It's really hard to write a good wait loop.

It is. :)

> Evaluating the condition in two places is a burden on the programmer.

The trade-off is more convoluted control-flow elsewhere. Pick your poison.

> (re)computing the timeout should happen just before calling wait to 
> avoid expense of e.g. calling nanoTime unnecessarily.

First time through there's nothing to recompute so that seems wrong to me.

> After returning from wait, it is very likely that the condition is now 
> satisfied, suggesting that the call to wait should be the last thing in 
> the loop body, and testing the condition should be the first thing.

If not for all the other things you're trying to deal with. :)

> Throwing TimeoutException is a last resort in j.u.c.  We prefer to 
> return a *special value* (false or null).

Well without adding even more context to the example you can't return 
anything. The exception just makes the control flow clear.

And this is just trying to be a basic example - not blatantly dangerous 
but not necessarily bullet-proof either. :)


More information about the core-libs-dev mailing list