Static Synchronization in Java

In the post Synchronization in Java it has already been discussed that every object in Java has a single lock (also called monitor) associated with it. When a thread enters a synchronized method or synchronized block it acquires that lock. All other threads attempting to execute the same code (in synchronized method or synchronized block) have to wait for the first thread to finish and release the lock.

But what about the scenario where you have more than one object of the same class. In that case two separate threads can acquire locks of these two objects and enter the synchronized method or synchronized block with those separate locks at the same time. If you don’t want that to happen then you need static synchronization in Java.

Static synchronization

As you must be knowing a variable or method marked as static in Java belongs to the type not to objects and such variable or method would be shared among the instances of the class. Same way static synchronization in Java is also at the class level not at instance level.

Example code

Let’s try to clarify static synchronization in Java with an example.

Here we have a class with two methods, in one of the method there is a for loop running from 1 to 5 and those values are displayed, in another method for loop runs from 5 to 1 and values are displayed.

Two objects of this class ctr1 and ctr2 are created. Four threads are also created out of which two share the ctr1 object where as the other two share the ctr2 object. What you need is that the threads are executed in such a way that the values in the loop are displayed in order i.e. 1,2,3,4,5 and 5,4,3,2,1. For that you have also synchronized the methods increment() and decrement().

Output

As you can see from the output since thread-0 and thread-1 are synchronized on the same object ctr1 so these two thread won’t interleave. Same way thread-2 and thread-3 are synchronized on the same object ctr2 so these two threads won’t interleave. But thread-2 can enter the synchronized method even when thread-0 has acquired a lock as they both have separate object locks of their own.

Using static synchronization

To fix this problem you need to use static synchronization to have synchronization at the class level not at the instance level. With static synchronization in Java, the thread acquires the intrinsic lock for the Class object associated with the class.

In the above example you just need to make methods increment() and decrement() as static too.

Output

static synchronized block in Java

You can also have static synchronized block in Java, rather than using the object reference you will have to synchronize the class object itself.

General form of static synchronized block in Java

Example code

In the above example you can change methods increment() and decrement() to have synchronized blocks with class reference.

That’s all for the topic Static Synchronization in Java. If something is missing or you have something to share about the topic please write a comment.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.