Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Are all monitors released when thread waits on an object?

Tags:

Before a thread can wait on an object, it has to acquire a monitor on that object. The monitor is then released, and the thread attempts to re-acquired it once it awakes.

But what happens to other monitors the thread holds when it calls wait?

Consider this example:

   Object a = // ...
   Object b = // ...

   synchronized(a)
   {
       synchronized(b)
       {
           b.wait();
           // continue
       }
   }

When the thread calls b.wait(), will it release the locks on both a and b, or only b?

like image 380
Tony the Pony Avatar asked Jun 07 '11 12:06

Tony the Pony


People also ask

What happens when thread wait?

When a thread calls wait() , it's temporarily releasing the monitor (lock) of the object until it receives a notification from another thread. This way, a thread can willingly give control (that it has, in the first place) of the object's monitor to another thread.

Can a thread wait on a monitor?

Monitors. Monitor is a synchronization construct that allows threads to have both mutual exclusion (using locks) and cooperation i.e. the ability to make threads wait for certain condition to be true (using wait-set).

What does thread wait do in Java?

In java, synchronized methods and blocks allow only one thread to acquire the lock on a resource at a time. So, when wait() method is called by a thread, then it gives up the lock on that resource and goes to sleep until some other thread enters the same monitor and invokes the notify() or notifyAll() method.

How are monitors implemented in Java?

Java's implementation of a monitor mechanism relies on two concepts – the entry set and the wait set. In literature, authors use a building and exclusive room analogy to represent the monitor mechanism. In this analogy, only one person can be present in an exclusive room at a time.


2 Answers

Only b.

The authoritarian source for these type of questions is the Java Language Specification. The relevant section in this case is 17.8 Wait Sets and Notification:

Let thread t be the thread executing the wait method on object m, and let n be the number of lock actions by t on m that have not been matched by unlock actions. One of the following actions occurs.

  • [...]
  • Otherwise, the following sequence occurs:

    1. Thread t is added to the wait set of object m, and performs n unlock actions on m.
    2. [...]
like image 76
aioobe Avatar answered Oct 19 '22 10:10

aioobe


From the Java API documentation of the Object class:

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

So, calling b.wait() releases the lock on b only.

like image 27
Vineet Reynolds Avatar answered Oct 19 '22 10:10

Vineet Reynolds