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

preview_player
Показать описание
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
Рекомендации по теме
Комментарии
Автор

I had faced this question in one of my interviews. Thanks for bringing this up.

sayanbiswas
Автор

actually now only I understand the double-checked locking of the singleton class, the way you explained the thread's behavior is Awesome. (except the last part cache). Thanks a ton for this great lecture. 🙂🙂🙂

Sai-pjs
Автор

Because of you guys I have become so much confident in Java ! I was good at dsa but you guys helped me a lot while cracking interviews of Accenture, Infosys, global logic and 7 others. Can't thank you enough

gyanprakash
Автор

Great Explanation!!! Thanks a lot.
Keep creating more such videos.

rekham
Автор

Awesome topic and explanation. Teach us more deep in this type of topics!!

akashsaha
Автор

9:30 your explanation always perfect. so easy to understand Thanks

sproutboot
Автор

Cannot wait for the second part!! 😊😊😊😊 You're awesome

manosijroy
Автор

Nicely explained 👏..ur springboot. Questions were the ones asked in one my interview..thanks for creating n explaining the same

nutankumari
Автор

As far as i know if a code block is synchronized then only one thread can access it even if there are multiple thread running. Once a thread leaves that block only then other thread can access that block. If i am not wrong then is contradicts the volatile part of the video. Please correct me if i am wrong. Also thanks a lot for your videos.

asifkamal
Автор

Hi Thanks for video could you please let me know in which
Scenario we will use singleton design pattern

prathyushamovva
Автор

Thank u for such a informative video 💯💯

lohir
Автор

Thanks mam for such informative videos 🙏🙏🙏
Please do video on spring profile concept in depth.

pratikkurbet
Автор

Hi, I'm trying to connect to Dbeaver which was shown one of your videos, but not able to establish connection.

Can you provide any help on it. Thanks

chaitanya
Автор

Thank you so much for your content.. can you please make a videos how reflection is internally used in spring?

sahukarinaveenkumar
Автор

Awesome explanation 👌👍 please make the next video on Kafka.

narasaiahboga
Автор

Mam can you please make video on loggers and logging levels.
Also on how to use ActiveMQ/ RabbitMQ

RockMB
Автор

At 17.03, I think It would be T1 Not T2. ( T1 was about to write a new singleton instance ), please correct me If I'm wrong.

VinitUpare
Автор

How is it possible for two threads to go in a one synchronized block?

sergeykuznetsov