Java Program to Demonstrate the Double-Check Locking For Singleton Class
Last Updated :
07 Jan, 2021
One of the key challenges faced by junior developers is the way to keep Singleton class as Singleton i.e. the way to prevent multiple instances of Singleton class. Double checked locking of Singleton is a way to make sure that only one instance of Singleton class is created through an application life cycle. In double-checked locking, code checks for an existing instance of Singleton class twice with and without locking to make sure that only one instance of singleton gets created.
Need of Double-checked Locking of Singleton Class:
Java
private static Singleton instance;
public static Singleton getInstance1()
{
if (instance == null ) {
instance = new Singleton();
}
return instance;
}
|
- The above code will create multiple instances of Singleton class if called by more than one thread in parallel(known as multithreading).
- The primary solution to the current problem will be to make getInstance() method synchronized.
- Though it’s thread-safe and solves the issue of multiple instances, it isn’t very efficient. You need to bear cost of synchronization every time you call this method, while synchronization is only needed on first class, when Singleton instance is created.
This brings us to double checked locking pattern, where only a critical section of code is locked.
Why is it called Double Checked Locking?
- It is called double-checked locking because there are two checks for instance == null, one without locking and other with locking (inside synchronized) block.
How Double Checked Locking looks like in Java:
Java
public static Singleton getInstance2()
{
if (instance == null ) {
synchronized (Singleton. class )
{
if (instance == null ) {
instance = new Singleton();
}
}
}
return instance;
}
|
Implementation double-checked locking on Singleton class:
Java
class Singleton {
private volatile static Singleton instance;
private Singleton() {}
public static Singleton getInstance1()
{
if (instance == null ) {
instance = new Singleton();
}
return instance;
}
public static synchronized Singleton getInstance2()
{
if (instance == null ) {
instance = new Singleton();
}
return instance;
}
public static Singleton getInstance3()
{
if (instance == null ) {
synchronized (Singleton. class )
{
if (instance == null ) {
instance = new Singleton();
}
}
}
return instance;
}
}
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...