Re: JavaMemoryModel: Code that sleeps and expects to see changes

From: Doug Lea (
Date: Mon Oct 25 1999 - 07:27:46 EDT

> The question is what does sleep do? Is it simple a most cost
> effective way of waiting for a certain number of milliseconds than a
> timing loop, or does it has synchronization semantics?

Again, I strongly suggest using the tactic I pursued in Describe the effects
of sleep and join in terms of Object.wait. Object.wait has clear
synch requirements. This approach does not force a particular
implementation; it merely says that any real implementation cannot be
distinguished from the one defined in terms of wait. This led me to
write it as:

  An invocation of method Thread.sleep(long millisecs) is not guaranteed
  to be behaviorally distinguishable from the action:

     if (millisecs != 0) {
        Object s = new Object();
        synchronized(s) { s.wait(millisecs); }

  Note that s is an object that is not otherwise used in any
  way. Thread.sleep(long millisecs, int nanosecs) operates identically
  except for accommodating nanosecond timing arguments.

When methods are specified in such ways, they can be defined
independently of memory model.

Doug Lea, Computer Science Department, SUNY Oswego, Oswego, NY 13126 USA 315-341-2688 FAX:315-341-5424  
JavaMemoryModel mailing list -

This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:21 EDT