filmov
tv
Double checked Locking in Singleton Design pattern | Java Interview Questions | Code Decode

Показать описание
In this video of code decode we have explained the double checked locking of singleton design pattern which is very important java interview question and answer
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
Course Description Video :
What is Singleton ? In which Scenerio it will break ?
private static Singleton instance;
public static Singleton getInstance1()
{
if (instance == null) {
instance = new Singleton();
}
return instance;
}
This was the code we used to implement the singleton class in java.
The above code will create multiple instances of Singleton class if called by more than one thread in parallel(known as multithreading)
Write an efficient code to implement singleton which prevents Your code from breaking under multi threaded conditions
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 time, when Singleton instance is created.
This brings us to double checked locking pattern, where only a critical section of code is locked.
Double checked locking pattern
It is called double-checked locking because there are two checks for instance == null, one without locking and other with locking (inside synchronized) block
if (instance == null) {
{
// Double checked
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
Double checked locking pattern
Here the Intention is to reduce cost of synchronization and improve performance, by only locking critical section of code, the code which creates instance of Singleton class.
Now only the first time code goes in sync block and for rest all the calls, the code is not synchronised and hence performance increases in this implementation
On the surface, this method looks perfect, as you only need to pay price for synchronized block one time, but it has still broken until you make instance variable volatile.
The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.
Subscriber and Follow Code Decode
#singletondoublecheckedlocking #codedecode #javainterviewquestion
Udemy Course of Code Decode on Microservice k8s AWS CICD link:
Course Description Video :
What is Singleton ? In which Scenerio it will break ?
private static Singleton instance;
public static Singleton getInstance1()
{
if (instance == null) {
instance = new Singleton();
}
return instance;
}
This was the code we used to implement the singleton class in java.
The above code will create multiple instances of Singleton class if called by more than one thread in parallel(known as multithreading)
Write an efficient code to implement singleton which prevents Your code from breaking under multi threaded conditions
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 time, when Singleton instance is created.
This brings us to double checked locking pattern, where only a critical section of code is locked.
Double checked locking pattern
It is called double-checked locking because there are two checks for instance == null, one without locking and other with locking (inside synchronized) block
if (instance == null) {
{
// Double checked
if (instance == null) {
instance = new Singleton();
}
}
}
return instance;
Double checked locking pattern
Here the Intention is to reduce cost of synchronization and improve performance, by only locking critical section of code, the code which creates instance of Singleton class.
Now only the first time code goes in sync block and for rest all the calls, the code is not synchronised and hence performance increases in this implementation
On the surface, this method looks perfect, as you only need to pay price for synchronized block one time, but it has still broken until you make instance variable volatile.
The Java volatile keyword is used to mark a Java variable as "being stored in main memory". More precisely that means, that every read of a volatile variable will be read from the computer's main memory, and not from the CPU cache, and that every write to a volatile variable will be written to main memory, and not just to the CPU cache.
Subscriber and Follow Code Decode
#singletondoublecheckedlocking #codedecode #javainterviewquestion
Комментарии