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, , let n integer such t n'th lexically enclosing type declaration of class declaration contains method invocation. target reference n'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.


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo