How a class level lock is committed

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?

+6
source share
2 answers

On similar grounds, which is used as a mutex when we acquire a class-level lock

The class object itself will be used as a mutex. The equivalent synchronized block for your static synchronized method will look like this:

 public static int getCountTwo() { synchronized(ClassName.class) { return count++; } } 

ClassName is the name of the class containing this method.

See JLS Section ยง8.4.3.6 :

The synchronized method receives the monitor (ยง17.1) before executing it.

For a class (static) method, the monitor associated with the class object for the method class .

For the instance method, the monitor associated with this (the object for which the method was called).

Emphasis is mine.

+10
source

Object Level Lock:

Object level locking is the mechanism when you want to synchronize a non-static method or non-static block of code so that only one thread can execute a block of code for a given instance of the class. This should always be done to ensure the safe flow of instance-level data. This can be done as follows:

 public class DemoClass { public synchronized void demoMethod(){} } or public class DemoClass { public void demoMethod(){ synchronized (this) { //other thread safe code } } } or public class DemoClass { private final Object lock = new Object(); public void demoMethod(){ synchronized (lock) { //other thread safe code } } 

Class Level Lock:

Class-level locking prevents multiple threads from being included in the synchronized block in any of the available instances at run time. This means that if at run time there are 100 instances of DemoClass, then only one thread will be able to execute demoMethod () in any instance at a time, and all other instances will be blocked for other threads. This should always be done to ensure the security of static data.

 public class DemoClass { public synchronized static void demoMethod(){} } or public class DemoClass { public void demoMethod(){ synchronized (DemoClass.class) { //other thread safe code } } } or public class DemoClass { private final static Object lock = new Object(); public void demoMethod(){ synchronized (lock) { //other thread safe code } } } 
0
source

Source: https://habr.com/ru/post/949970/


All Articles