java - How class level lock is acquired -


public synchronized int getcountone() {         return count++;  } 

like in above code synchronizing on method functionally equivalent having synchronized (this) block around body of method. object "this" doesn't become locked, rather object "this" used mutex , body prevented executing concurrently other code sections synchronized on "this."

on similar grounds used mutex when acquire class level lock.as in if have function

public static synchronized int getcounttwo() {         return count++;  } 

obviously 2 threads can simultaneously obtain locks on getcountone(object level lock) , getcounttwo(class level lock). getcountone analogous

public int getcountone() {      synchronized(this) {           return count++;      }  } 

is there equivalent of getcounttwo? if no criteria used obtain class level lock?

on similar grounds used mutex when acquire class level lock

the class object used mutex. equivalent synchronized block static synchronized method like:

public static int getcounttwo() {      synchronized(classname.class) {           return count++;      } } 

classname name of class containing method.

see jls section §8.4.3.6:

a synchronized method acquires monitor (§17.1) before executes.

for class (static) method, the monitor associated class object method's class used.

for instance method, monitor associated (the object method invoked) used.

emphasis mine.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -