Java Synchronized Block for .class

JavaMultithreadingSynchronizationLockingSynchronized

Java Problem Overview


What does this java code mean? Will it gain lock on all objects of MyClass?

synchronized(MyClass.class) {
   //is all objects of MyClass are thread-safe now ??
}

And how the above code differs from this one:

synchronized(this) {
   //is all objects of MyClass are thread-safe now ??
}

Java Solutions


Solution 1 - Java

The snippet synchronized(X.class) uses the class instance as a monitor. As there is only one class instance (the object representing the class metadata at runtime) one thread can be in this block.

With synchronized(this) the block is guarded by the instance. For every instance only one thread may enter the block.

synchronized(X.class) is used to make sure that there is exactly one Thread in the block. synchronized(this) ensures that there is exactly one thread per instance. If this makes the actual code in the block thread-safe depends on the implementation. If mutate only state of the instance synchronized(this) is enough.

Solution 2 - Java

To add to the other answers:

static void myMethod() {
  synchronized(MyClass.class) {
    //code
  }
}

is equivalent to

static synchronized void myMethod() {
  //code
}

and

void myMethod() {
  synchronized(this) {
    //code
  }
}

is equivalent to

synchronized void myMethod() {
  //code
}

Solution 3 - Java

No, the first will get a lock on the class definition of MyClass, not all instances of it. However, if used in an instance, this will effectively block all other instances, since they share a single class definition.

The second will get a lock on the current instance only.

As to whether this makes your objects thread safe, that is a far more complex question - we'd need to see your code!

Solution 4 - Java

Yes it will (on any synchronized block/function).

I was wondering about this question for couple days for myself (actually in kotlin). I finally found good explanation and want to share it:

Class level lock prevents multiple threads to enter in synchronized block in any of all available instances of the class on runtime. This means if in runtime there are 100 instances of DemoClass, then only one thread will be able to execute demoMethod() in any one of instance at a time, and all other instances will be locked for other threads.

Class level locking should always be done to make static data thread safe. As we know that static keyword associate data of methods to class level, so use locking at static fields or methods to make it on class level.

Plus to notice why .class. It is just because .class is equivalent to any static variable of class similar to:

private final static Object lock = new Object();

where lock variable name is class and type is Class<T>

Read more: https://howtodoinjava.com/java/multi-threading/object-vs-class-level-locking/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser249739View Question on Stackoverflow
Solution 1 - JavaThomas JungView Answer on Stackoverflow
Solution 2 - JavaJornView Answer on Stackoverflow
Solution 3 - JavaDavid MView Answer on Stackoverflow
Solution 4 - JavavitaliiView Answer on Stackoverflow