Reputation: 415
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
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
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:
x.m1(y)
where x
and y
are potentially different instances of Class
.m1
, the JVM locks the monitor for x
.m1
invokes m2
m2
the JVM locks the monitor for y
.m2
exits, the monitor for y
is releasedm1
exits, the monitor for x
is releasedIf x
and y
are the same object, then the lock is released when m1
exits.
Upvotes: 3