public synchronized int getCountOne() { return count++; }
As in the previous code, method synchronization is functionally equivalent to having a synchronized (this) block around the method body. The "this" object is not locked, and the "this" object is used as mutex , and the body cannot be executed simultaneously with other sections of the code that are also synchronized with "this".
Similar to what is used as mutex when we acquire a class-level lock. In the event that we have a function
public static synchronized int getCountTwo() { return count++; }
it is obvious that two threads can simultaneously receive locks on getCountOne (object-level lock) and getCountTwo (class-level lock). Since getCountOne is similar
public int getCountOne() { synchronized(this) { return count++; } }
is there an equivalent to getCountTwo? If any criteria are not used to get class level locks?
source share