java - Synchronized block equivalent to static synchronized method? -


when have method such following:

public synchronized void addone() {     a++; } 

it equivalent following: (correct me if i'm wrong)

public void addone() {     synchronized(this) {         a++;     } } 

but equivalent following method?:

public static synchronized void addone() {     a++;     // (in case 'a' must static) } 

what synchronized block acts same static synchronized method? understand static synchronized method synchronized on class , not instance (since there no instance), syntax that?

it equivalent locking on class object. can reference class object writing class name followed .class. so, like:

synchronized(yourclass.class) { } 

see java language specification, section 8.4.3.6 synchronized methods:

a synchronized method acquires lock (§17.1) before executes. class (static) method, lock associated class object method's class used. instance method, lock associated (the object method invoked) used.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -