Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Because the method is synchronized, if the thread is suspended, other threads are unable to use the synchronized methods of the class. In other words, the The current object's monitor is not released . This is because the Thread.sleep() method does not have any synchronization semantics, as detailed in CON16-J. Do not expect sleep(), yield() and getState() methods to have any synchronization semantics.

...

Code Block
bgColor#ccccff
private synchronized void doSomething(long timeout)
  throws InterruptedException {
 
  while (<condition does not hold>) {
    wait(timeout); // Immediately releasesleaves lock on current monitor
  }
}

The current object's monitor is immediately released upon entering the wait state. After the time out period has elapsed, the thread attempts to reacquire the current object's monitor and resumes execution when it succeeds.

...

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits. This method should only be called by a thread that is the owner of this object's monitor.

Consequently, ensure Ensure that a thread that holds locks on other objects releases them appropriately, before entering the wait state. Also, refer to the related guidelines CON18-J. Always invoke wait() and await() methods inside a loop and CON19-J. Notify all waiting threads instead of a single thread.

...