MarsaPalas
MarsaPalas

Reputation: 415

locks and nested synchronized methods

Say we have something like this,

class Class{    
//...
synchronized void m1(Class obj){ obj.m2(); }
synchronized void m2(){ /*...*/ }
}

My question - when is lock of an object obj released? When it returns from method m2, or method m1?

Upvotes: 1

Views: 1362

Answers (2)

Costi Ciudatu
Costi Ciudatu

Reputation: 38235

Try to translate the synchronized methods in synchronized blocks:

m1(C obj) {
    synchronized(this) {
        obj.m2();
    }
}

m2() {
    synchronized(this) {
        // some stuff
    }
}

So your obj Object is only locked during the m2() call. This is because that's the only time when this translates to obj.

The m1() call only locks the current object, which by the way could also be the same obj; if this is the case (this == obj in the first call), then obj is locked until both m1() and m2() complete.

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

From the JLS Section 17

A thread t may lock a particular monitor multiple times; each unlock reverses the effect of one lock operation.

However, your question is complicated by the fact that m1() and m2() may lock different objects. m1() locks whatever object you invoked it on, which you haven't shown. So the sequence of events is:

  1. You invoke x.m1(y) where x and y are potentially different instances of Class.
  2. Before entering m1, the JVM locks the monitor for x.
  3. m1 invokes m2
  4. Before entering m2 the JVM locks the monitor for y.
  5. When m2 exits, the monitor for y is released
  6. When m1 exits, the monitor for x is released

If x and y are the same object, then the lock is released when m1 exits.

Upvotes: 3

Related Questions