Java Synchronized Block for .class
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.
synchronized(this) vs synchronized(MyClass.class)
MyClass.class
and this
are different things, they are different references to different objects.
this
- is a reference to this particular instance of the class, and
MyClass.class
- is a reference to the MyClass
description object.
These synchronization blocks differ in that the first will synchronize all threads that deal concretely with this instance of MyClass
, and the second one will synchronize all threads independently of which object on which method was called.
Reflection class in synchronized block
JVM has a monitor or otherwise called intristic flag related with the class meta information which are stored in some specific place in memory (permGen or metaspace). In that place a single monitor flag exists and is related with the class it self and not some instance of that class.
synchronized (Singleton.class)
will capture this flag in permgen memory (before jdk 8) or in metaspace memory (jdk 8 and after) .
JVM also has a separate monitor flag for every class instance which is stored in heap.
synchronized (this)
will capture the flag of a specific instance in heap memory which is pointed by this
when that instance was invoked.
Those flags work in the same way but are just different flags. So only 1 thread can require to capture the flag and other threads should wait for this flag to be released before they can capture it and access the code. It is up to you and your requirements if you lock the static flag or the instance flag but the concept is the same. Not 2 threads can hold the same flag at the same time. 1 will block and wait for the flag to be released before it can access the synchronized code.
Note: this
will not work inside a static method. In this case you have available only the flag related with the class it self. You can still though use both of those type of flags in non static method.
Related Topics
How to Ensure Hashcode() Is Consistent with Equals()
Java Error - Actual and Formal Argument Lists Differ in Length
How to Build Sources Jar with Gradle
Getting a Illegalblocksizeexception: Data Must Not Be Longer Than 256 Bytes When Using Rsa
Why Doesn't Java Allow Generic Subclasses of Throwable
Jpa/Hibernate Native Queries Do Not Recognize Parameters
Split List into Multiple Lists with Fixed Number of Elements in Java 8
How to Make Image Appear Randomly Every X Seconds in Java Using Timer
Replacing All Non-Alphanumeric Characters with Empty Strings
Using Powermockito.Whennew() Is Not Getting Mocked and Original Method Is Called
Comparing Float/Double Values Using == Operator
Java Synchronized Block for .Class
Create a Autocompleting Textbox in Java with a Dropdown List
Retrieving a Random Item from Arraylist
How to Read the Manifest File for a Webapp Running in Apache Tomcat
Get the Changed HTML Content After It's Updated by JavaScript? (Htmlunit)
Swingworker, Thread.Sleep(), or Javax.Swing.Timer? I Need to "Insert a Pause"