Java Program to Demonstrate the Double-Check Locking For Singleton Class Last Updated : 07 Jan, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report One of the key challenges faced by junior developers is the way to keep Singleton class as Singleton i.e. the way to prevent multiple instances of Singleton class. Double checked locking of Singleton is a way to make sure that only one instance of Singleton class is created through an application life cycle. In double-checked locking, code checks for an existing instance of Singleton class twice with and without locking to make sure that only one instance of singleton gets created. Need of Double-checked Locking of Singleton Class: Java private static Singleton instance; public static Singleton getInstance1() { if (instance == null) { instance = new Singleton(); } return instance; } The above code will create multiple instances of Singleton class if called by more than one thread in parallel(known as multithreading). 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 class, when Singleton instance is created. This brings us to double checked locking pattern, where only a critical section of code is locked. Why is it called Double Checked Locking? It is called double-checked locking because there are two checks for instance == null, one without locking and other with locking (inside synchronized) block. How Double Checked Locking looks like in Java: Java public static Singleton getInstance2() { // Single Checked if (instance == null) { synchronized (Singleton.class) { // Double checked if (instance == null) { instance = new Singleton(); } } } return instance; } Implementation double-checked locking on Singleton class: Java // Java Program to write double checked locking // of Singleton class class Singleton { private volatile static Singleton instance; private Singleton() {} // 1st version: creates multiple instances if two thread // access this method simultaneously public static Singleton getInstance1() { if (instance == null) { instance = new Singleton(); } return instance; } // 2nd version : this is thread-safe and only // creates one instance of Singleton on concurrent // environment but it is unnecessarily expensive due to // cost of synchronization at every call. public static synchronized Singleton getInstance2() { if (instance == null) { instance = new Singleton(); } return instance; } // 3rd version : An implementation of double checked // locking of Singleton. 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. public static Singleton getInstance3() { if (instance == null) { synchronized (Singleton.class) { if (instance == null) { instance = new Singleton(); } } } return instance; } } Comment More infoAdvertise with us Next Article How to Check if a Thread Holds Lock on a Particular Object in Java? K kashish2008 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Demonstrate the Lazy Initialization Non-Thread-Safe In recent years object-oriented programming has formed the pillars/bases of Website and Application (Software) Development. Java and Python are popular Object-Oriented programming languages out of which the former one is offered and maintained by Oracle, while the latter one is open-source. Java alo 8 min read Java Program to Demonstrate the Lazy Initialization Thread-Safe Java is a popular object-oriented programming language used by developers and coders for website/app development. The creation of a class object using a new keyword is referred to as object instantiation. Java by default allows users to define two ways of object instantiation which are Eager and Laz 7 min read How to Implement File Locking in Java to Prevent Concurrent Access ? In Java, Concurrent file access may cause corruption and errors in the data. File locking is used to stop many threads or processes from concurrently accessing the same file. A simple method for implementing file locking in Java is the FileLock class, which comes under java.nio.channels package. The 2 min read How to Check if a Thread Holds Lock on a Particular Object in Java? Java language is one of the most popular languages for many years. One of the most advantageous features of java programming is Multithreading. Multithreading allows executing a single program into many small parts of the program with the maximum utilization of the CPU. Threads are a lightweight pro 3 min read How to Handle Concurrent Access and Modification of a PriorityQueue in Java? In Java, PriorityQueue is a class that implements a priority queue based on the priority heap. While PriorityQueue provides efficient operations for the adding and removing elements and it is not inherently thread-safe. Concurrent access and modification of the PriorityQueue can lead to unexpected b 3 min read How to Use Locks in Multi-Threaded Java Program? A lock may be a more flexible and complicated thread synchronization mechanism than the standard synchronized block. A lock may be a tool for controlling access to a shared resource by multiple threads. Commonly, a lock provides exclusive access to a shared resource: just one thread at a time can ac 6 min read Like