No video

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

  Рет қаралды 20,156

Code Decode

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:
openinapp.co/u...
Course Description Video :
yt.openinapp.c...
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 a...
Advance Java Interview Questions and Answers : • Advance Java Interview...
Java 8 Interview Questions and Answers : • Java 8 Interview Quest...
Hibernate Interview Questions and Answers : • Hibernate Interview Qu...
Spring Boot Interview Questions and Answers : • Advance Java Interview...
Angular Playlist : • Angular Course Introdu...
SQL Playlist : • SQL Interview Question...
GIT : • GIT
Subscriber and Follow Code Decode
Subscriber Code Decode : www.youtube.co...
LinkedIn : / codedecodeyoutube
Instagram : / codedecode25
#singletondoublecheckedlocking #codedecode #javainterviewquestion

Пікірлер: 55
@sayanbiswas8847
@sayanbiswas8847 2 жыл бұрын
I had faced this question in one of my interviews. Thanks for bringing this up.
@CodeDecode
@CodeDecode 2 жыл бұрын
🙂 yeah its one of the imp concept of java. Its usually asked to check clarity on multiple topics of java like volatile, multithreading etc
@user-od8cv2wo7u
@user-od8cv2wo7u Жыл бұрын
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. 🙂🙂🙂
@CodeDecode
@CodeDecode Жыл бұрын
Thanks 😊👍
@akashsaha9366
@akashsaha9366 2 жыл бұрын
Awesome topic and explanation. Teach us more deep in this type of topics!!
@CodeDecode
@CodeDecode 2 жыл бұрын
Thanks akash 🙂👍sure 👍👍
@sproutboot
@sproutboot Жыл бұрын
9:30 your explanation always perfect. so easy to understand Thanks
@CodeDecode
@CodeDecode Жыл бұрын
Thanks a lot lee 🙂🙂
@ganpatinatrajan5890
@ganpatinatrajan5890 Жыл бұрын
Excellent Explanations...👍👍👍
@CodeDecode
@CodeDecode Жыл бұрын
Thanks Ganpati 😊👍
@manosij00roy
@manosij00roy 2 жыл бұрын
Cannot wait for the second part!! 😊😊😊😊 You're awesome
@CodeDecode
@CodeDecode 2 жыл бұрын
sure we will create it soon
@rekham4583
@rekham4583 2 жыл бұрын
Great Explanation!!! Thanks a lot. Keep creating more such videos.
@CodeDecode
@CodeDecode 2 жыл бұрын
Thanks rekha for the nice words
@gyanprakash302
@gyanprakash302 2 жыл бұрын
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
@CodeDecode
@CodeDecode 2 жыл бұрын
Many congratulations Gyan 🎉🎉👏👏well deserved 👍👍🙂🙂keep learning keep shining Man ⭐✨⭐✨
@shankardayal6928
@shankardayal6928 2 жыл бұрын
Please help me so that I can master DSA
@gyanprakash302
@gyanprakash302 2 жыл бұрын
@@shankardayal6928 Keep practicing from gfg and leetcode bro
@nutankumari9977
@nutankumari9977 2 жыл бұрын
Nicely explained 👏..ur springboot. Questions were the ones asked in one my interview..thanks for creating n explaining the same
@CodeDecode
@CodeDecode 2 жыл бұрын
Thanks nutan for the nice words
@harikrishnab4930
@harikrishnab4930 2 жыл бұрын
Good explanation. Thank you.
@CodeDecode
@CodeDecode 2 жыл бұрын
you're welcome
@lohir446
@lohir446 2 жыл бұрын
Thank u for such a informative video 💯💯
@CodeDecode
@CodeDecode 2 жыл бұрын
Thanks 🙂👍
@amarthyaseshu683
@amarthyaseshu683 2 жыл бұрын
Great explanation 🤠
@CodeDecode
@CodeDecode 2 жыл бұрын
Thanks Amarthya
@prathyushamovva5718
@prathyushamovva5718 8 ай бұрын
Hi Thanks for video could you please let me know in which Scenario we will use singleton design pattern
@pratikkurbet3437
@pratikkurbet3437 2 жыл бұрын
Thanks mam for such informative videos 🙏🙏🙏 Please do video on spring profile concept in depth.
@CodeDecode
@CodeDecode 2 жыл бұрын
Sure Pratik 🙂👍
@asifkamal166
@asifkamal166 8 ай бұрын
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.
@sahukarinaveenkumar3188
@sahukarinaveenkumar3188 Жыл бұрын
Thank you so much for your content.. can you please make a videos how reflection is internally used in spring?
@bharukeerthi1986
@bharukeerthi1986 Жыл бұрын
Great🎉
@CodeDecode
@CodeDecode Жыл бұрын
Thanks 🙂❤
@VinitUpare
@VinitUpare 13 күн бұрын
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.
@narasaiahboga3579
@narasaiahboga3579 2 жыл бұрын
Awesome explanation 👌👍 please make the next video on Kafka.
@CodeDecode
@CodeDecode 2 жыл бұрын
kzfaq.info/sun/PLyHJZXNdCXset6tBB1aY5aEY77YhbxWLo
@narasaiahboga3579
@narasaiahboga3579 2 жыл бұрын
I already watched these videos. I'm requesting you for kakfa implementation videos
@RockMB
@RockMB 2 жыл бұрын
Mam can you please make video on loggers and logging levels. Also on how to use ActiveMQ/ RabbitMQ
@CodeDecode
@CodeDecode 2 жыл бұрын
Sure Mayur 🙂👍
@jayarajsg4800
@jayarajsg4800 Жыл бұрын
Thanks!
@CodeDecode
@CodeDecode Жыл бұрын
You're welcome
@chaitanya368
@chaitanya368 2 жыл бұрын
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
@naleenyadav
@naleenyadav Жыл бұрын
Thanks !!
@CodeDecode
@CodeDecode Жыл бұрын
you're welcome
@sergeykuznetsov3249
@sergeykuznetsov3249 Жыл бұрын
How is it possible for two threads to go in a one synchronized block?
@CodeDecode
@CodeDecode Жыл бұрын
Only 1 will go at a time
@motivation_hubPJ
@motivation_hubPJ Жыл бұрын
can you please answer this as you explain the use case of volatile but that should only arise when two thread's are accessing the same block at same time or preemption is happening but in any case the synchronised block must now allow the other thread to access because of the lock mechanism hence i am unable figure why you may want to use volatile here
@motivation_hubPJ
@motivation_hubPJ Жыл бұрын
@CodeDecode
@UmmakaJagadish
@UmmakaJagadish 2 жыл бұрын
expecting new video madam
@CodeDecode
@CodeDecode 2 жыл бұрын
sure we will create it soon
@ThushanVithana
@ThushanVithana 2 жыл бұрын
elbowcoughawesome yt
@CodeDecode
@CodeDecode 2 жыл бұрын
🙂🙂
@sagart9316
@sagart9316 Жыл бұрын
Aaa
@ThushanVithana
@ThushanVithana 2 жыл бұрын
awsome
@CodeDecode
@CodeDecode 2 жыл бұрын
Thanks Thushan 🙂👍
If Barbie came to life! 💝
00:37
Meow-some! Reacts
Рет қаралды 47 МЛН
Sunglasses Didn't Cover For Me! 🫢
00:12
Polar Reacts
Рет қаралды 5 МЛН
Spot The Fake Animal For $10,000
00:40
MrBeast
Рет қаралды 210 МЛН
Singleton and Double Checked Locking
7:57
Defog Tech
Рет қаралды 31 М.
Top 25 Microservice Interview Questions Answered - Java Brains
39:54
How to BREAK and FIX Singleton Design Pattern | Interview Question
19:32
Daily Code Buffer
Рет қаралды 31 М.
8 Design Patterns EVERY Developer Should Know
9:47
NeetCode
Рет қаралды 1 МЛН
13. Java Singleton and Immutable Class Explained with Examples | Java Classes in Depth - Part4
28:26
If Barbie came to life! 💝
00:37
Meow-some! Reacts
Рет қаралды 47 МЛН