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: 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 : https://youtube.com/playlist?list=PLyHJZXNdCXscoyL5XEZoHHZ86_6h3GWE1 Advance Java Interview Questions and Answers : https://youtube.com/playlist?list=PLyHJZXNdCXsexOO1VQ4vs-BM2-8CKrixd Java 8 Interview Questions and Answers : https://youtube.com/playlist?list=PLyHJZXNdCXsdeusn4OM33415DCMQ6sUKy Hibernate Interview Questions and Answers : https://youtube.com/playlist?list=PLyHJZXNdCXsdC-p2186C6NO4FpadnCC_q Spring Boot Interview Questions and Answers : https://youtube.com/playlist?list=PLyHJZXNdCXsexOO1VQ4vs-BM2-8CKrixd Angular Playlist : https://www.youtube.com/watch?v=CAl7RQSdq2Q&list=PLyHJZXNdCXsfxRtDwtGkDD_lLfTWc1g0i SQL Playlist : https://www.youtube.com/playlist?list=PLyHJZXNdCXse86eLuwy5uZohd_bddE9Ni GIT : https://youtube.com/playlist?list=PLyHJZXNdCXscpl6pxOnL2lRWJlzvzjyZE Subscriber and Follow Code Decode Subscriber Code Decode : https://www.youtube.com/c/CodeDecode?sub_confirmation=1 LinkedIn : https://www.linkedin.com/in/codedecodeyoutube/ Instagram : https://www.instagram.com/codedecode25/ #singletondoublecheckedlocking #codedecode #javainterviewquestion
Download
0 formatsNo download links available.