java - Calling outer class' syncronized method from inner class -
i have program looks in essence this
class outer { class inner implements runnable { public void run() { dosomething(); } } public synchronized void dosomething() { //... } }
which lock inner.run()
acquire when calling dosomething()
? identical synchronized(inner.this)
or synchronized(outer.this)
?
thanks lot in advance.
the receiver invocation of dosomething()
within run()
outer.this
. synchronized
therefore lock monitor on object referenced expression.
on computing target reference in method invocation expression, jls says
otherwise, let
t
enclosing type declaration of method member, , letn
integer sucht
n'th
lexically enclosing type declaration of class declaration contains method invocation. target referencen'th
lexically enclosing instance of this.
t
here outer
, since that's class declares it. n
1, outer
enclosing type declaration of inner
. target reference therefore 1'th lexically enclosing instance of this
, ie. outer.this
.
concerning synchronized
methods, jls says
for instance method, monitor associated
this
(the object method invoked) used.