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

Code Decode
Code Decode
19.8 هزار بار بازدید - 2 سال پیش - In this video of code
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:
https://openinapp.co/udemycourse

Course Description Video :
https://yt.openinapp.co/dmjvd


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) {
       synchronized (Singleton.class)
       {
           // 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.



Most Asked Core Java Interview Questions and Answers : Core Java frequently asked Interview ...

Advance Java Interview Questions and Answers : Advance Java Interview Questions

Java 8 Interview Questions and Answers : Java 8 Interview Questions(New Features)

Hibernate Interview Questions and Answers : Hibernate Interview Questions Java

Spring Boot Interview Questions and Answers : Advance Java Interview Questions

Angular Playlist :  Angular Course Introduction || Angular 8


SQL Playlist : SQL Interview Questions and Answers

GIT : GIT

Subscriber and Follow Code Decode
Subscriber Code Decode : https://www.seevid.ir/c/CodeDecode?...
LinkedIn : LinkedIn: codedecodeyoutube
Instagram : Instagram: codedecode25

#singletondoublecheckedlocking #codedecode #javainterviewquestion
2 سال پیش در تاریخ 1401/05/18 منتشر شده است.
19,823 بـار بازدید شده
... بیشتر