What is class level locking in java

What is class level locking. Could you explain by example.

+4
source share
5 answers

I assume that you mean synchronization lock. If you are not familiar with synchronization, this means that the same code is simultaneously launched by two different threads. In java synchronization, it is performed using object locks:

Object lock = new Object(); public void doSomething(){ ... synchronized(lock){ //something dangerous } ... } 

This code ensures that only one thread can do something dangerous at a given time, and it will complete everything in a synchronized block before another thread can run the same code. I assume that you call "class level locking" - this is an implicit locking by a synchronized method:

 public synchronized void somethingDangerous(){...} 

Here again, only one thread can execute the method at any given time and will always be finished before another thread can start executing the code. This is equivalent to a synchronized block on "this":

 public void somethingDangerous(){ synchronized(this){ //something dangerous } } 

Now this lock is not actually on the class, but on one instance (i.e. not on all watches, but only on your watch). If you need a true “class level lock” (usually done by the static method), you need to synchronize something regardless of any instances. For instance:

 public class Clock{ private static Object CLASS_LOCK = new Object(); public static void doSomething(){ ... synchronized(CLASS_LOCK){ //something dangerous to all clocks, not just yours } ... } } 

again there is an implicit lock for static methods:

 public class Clock{ public static synchronized void somethingDangerous(){} } 

which is the equivalent of locking an object of a class:

 public class Clock{ public static void somethingDangerous(){ synchronized(Clock.class){ //do something dangerous } } } 
+22
source

If you use the synchronized using the static method, the object whose monitor is used to lock is the class object, that is, the one represented by the literal MyClass.class . It is also used implicitly for synchronization when initializing the class itself.

+4
source

It is called an internal lock, and every object has it. We need to separate two things, and it is not entirely clear what you mean by “class-level locking”.

Here is an example:

 class Locker { public void x() { lock(this) { ... } } public void y() { lock(this) { ... } } } 

As you can see, both methods use "this" as the lock target - this way you can guarantee that they will never alternate. You can achieve this implicitly with the sync keyword:

 class Locker { public synchronized void x() { ... } public synchronized void y() { ... } } 

Two examples are the same at the bytecode level.

However, by locking classes, you can mean locking the actual class object, not an instance of that object. The way synchronized static methods work.

+2
source

Class level locking and instance level locking are different, mutually exclusive. Both do not interfere with each other. If one instance of the class is already blocked by the thread, then the other thread cannot obtain a lock for this instance until the lock is released by the first thread.

The same behavior exists for class level locking.

But if a thread gets a Class-level lock, then another thread can get a lock on one of its instances. Both can work in parallel.

0
source

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


All Articles