Everything About Java Synchronization

Posted By : Navin Purohit | 30-Oct-2018

 

 

 

Synchronization is an important concept in Java because of multi-threaded language where multiple threads run in parallel to complete program execution. In any multi-threaded environment synchronization of object or synchronization of the class is very important. In Java, Synchronization is possible by using keywords "synchronized" and "volatile”.



Concurrent access of shared resources increase the chance of errors: memory consistency errors and thread interference and to avoid these kinds of errors we need to properly synchronize our Java object to allow mutual exclusive access of critical section to multiple threads.


 

Reason to use Synchronization in Java?

In a multi-threaded environment, we need synchronization for objects and class, which are shared among multiple threads, to avoid any unexpected behavior. Synchronization in Java will only be needed if the shared object is not immutable. If the shared object is either read-only or immutable object, then there is no need of synchronization, in spite of multiple threads. JVM make sure one thread run at a time to execute Java synchronized code.



Synchronized Keyword provides following functionality required for concurrent programming:



1) The synchronization provides locking in java, which guarantees mutually exclusive access to the shared resource and prevents data corruption.

2) Synchronized keyword also avoid reordering of code statement by the compiler which will result from a concurrent issue if we don't use synchronized or volatile keyword.

3) Synchronized keyword requires locking and unlocking. At the time of entering into synchronized method or block thread needs to acquire the lock, at this time it reads data from main memory than cache and when it releases the lock, it cleans the write operation into main memory which eliminates memory inconsistency issues.

 

Synchronized keyword

Before Java 1.5 synchronized keyword was the only way to provide synchronization of shared objects in Java. By using a synchronized block or enclosed inside a synchronized method will be mutually exclusive, and only be executed by one thread at a time. We can have both static and nonstatic synchronized method and synchronized blocks in Java but we can not have synchronized variable and constructor in java. Using synchronized keyword with a variable and constructor is illegal and will result in compilation error.

 

Volatile keyword

Instead of using the synchronized keyword with a variable in Java, we can use volatile keyword with a variable, which will instruct JVM threads to read the value of the variable from main memory, rather than cached it locally. Block synchronization is preferred over method synchronization in Java because, in block synchronization, we only need to lock the critical section of code instead of the complete method. Since synchronization in Java comes with the cost of performance, we need to synchronize only part of the code which needs to be synchronized.


 

Example of Synchronized method:-


public class Counter{

private static int count = 0;

public static synchronized int getCount(){

return count;

}

public synchoronized setCount(int count){

this.count = count;

}

}


In the above example, the use of the Synchronized method is not properly used as both method are not getting locked on the same object. getCount() method is getting locked on Counter.class object while setCount() method is getting locked on current object. To make above code correct we have to either both method static or non-static or can use Synchronized block instead of the Synchronized method.


 

Example of Synchronized block:-

 

 

public class Singleton{
private static volatile Singleton _instance;

public static Singleton getInstance(){

if(_instance == null){

synchronized(Singleton.class){

if(_instance == null)

_instance = new Singleton();

}

}

return _instance;

}
 
In above example, we are showing the use of Synchronized block in java over a classic example of double checked locking in Singleton.

 

 

 

 

 

 

About Author

Author Image
Navin Purohit

Navin has an experience of 4 years in Java and NodeJS. He is eager to learn new technologies and want to explore his inner strengths.

Request for Proposal

Name is required

Comment is required

Sending message..