No, since you are obtaining lock on the SearchBox.class, only one thread will enter the synchronized block at a time. So the first thread enters then finds searchBox is null and creates it and then leaves the synchronized block, then the second thread enter the block then it finds that the searchBox is not null because the first thread already created it so it will not create a new instance of searchBox.

The double checked pattern is used to avoid obtaining the lock every time the code is executed. If the call are not happening together then the first condition will fail and the code execution will not execute the locking thus saving resources.

Answer from Arun P Johny on Stack Overflow
Top answer
1 of 4
92

No, since you are obtaining lock on the SearchBox.class, only one thread will enter the synchronized block at a time. So the first thread enters then finds searchBox is null and creates it and then leaves the synchronized block, then the second thread enter the block then it finds that the searchBox is not null because the first thread already created it so it will not create a new instance of searchBox.

The double checked pattern is used to avoid obtaining the lock every time the code is executed. If the call are not happening together then the first condition will fail and the code execution will not execute the locking thus saving resources.

2 of 4
36

Let's look at this code:

1 if (searchBox == null) {
2     synchronized (SearchBox.class) {
3         if (searchBox == null) {
4             searchBox = new SearchBox();
5         }
6     }
7 }

Let's try to reason about this. Let's say we have two threads A and B and let's assume that at least one of them reaches line 3 and observes searchBox == null is true. Two threads can not both be at line 3 at the same time because of the synchronized block. This is the key to understanding why double-checked locking works. So, it must the case that either A or B made it through synchronized first. Without loss of generality, say that that thread is A. Then, upon seeing searchBox == null is true, it will enter the body of the statement, and set searchBox to a new instance of SearchBox. It will then eventually exit the synchronized block. Now it will be B's turn to enter: remember, B was blocked waiting for A to exit. Now when it enters the block, it will observe searchBox. But A will have left just having set searchBox to a non-null value. Done.

By the way, in Java, the best way to implement a singleton is to use a single-element enum type. From Effective Java:

While this approach has yet to be widely adopted, a single-element enum type is the best way to implement a singleton.

🌐
Baeldung
baeldung.com › home › java › double-checked locking with singleton
Double-Checked Locking with Singleton | Baeldung
March 11, 2026 - This pattern reduces the number of lock acquisitions by simply checking the locking condition beforehand. As a result of this, there’s usually a performance boost. However, it should be noted that the double-checked locking is broken prior ...
Discussions

What is an effective way to use singleton pattern & double checked locking in java?
Don't, use an enum . More on reddit.com
🌐 r/java
13
5
January 6, 2017
Why we need volatile field with double-checked Singleton pattern?
I had a re-read about this; and to put it simply the idea of the values being read-to-from main memory is wrong really. What happens with a read of a volatile variable, is actually when you go to read, it waits for all pending writes to be completed. This is then fixes the issue. To understand it better look up memory barriers. More on reddit.com
🌐 r/javahelp
16
5
August 18, 2019
🌐
Blogger
javarevisited.blogspot.com › 2014 › 05 › double-checked-locking-on-singleton-in-java.html
Double Checked Locking on Singleton Class in Java - Example
This will bring us to double-checked locking pattern, where the only critical section of code is locked. Programmer call it double checked locking because there are two checks for _instance == null, one without locking and other with locking ...
🌐
University of Maryland Department of Computer Science
cs.umd.edu › ~pugh › java › memoryModel › DoubleCheckedLocking.html
The "Double-Checked Locking is Broken" Declaration
Just define the singleton as a static field in a separate class. The semantics of Java guarantee that the field will not be initialized until the field is referenced, and that any thread which accesses the field will see all of the writes resulting from initializing that field. Although the double-checked locking idiom cannot be used for references to objects, it can work for 32-bit primitive values (e.g., int's or float's).
🌐
Java67
java67.com › 2016 › 04 › why-double-checked-locking-was-broken-before-java5.html
How to Implement Double Checked Locking Singleton Pattern in Java? Example Tutorial | Java67
See Applying Concurrency and ... not a bottleneck then the simplest way to avoid Double-checked locking is to make the whole method synchronized....
🌐
Java67
java67.com › 2015 › 09 › thread-safe-singleton-in-java-using-double-checked-locking-pattern.html
Thread-safe Singleton in Java using Double Checked Locking Idiom | Java67
Now, thread T1 wake up and creates a new instance of singleton and come out of synchronized block. After this when thread T2 goes inside synchronized block, it again checks whether _instance is null and this time check fails because _instnace is no more null. So thread T2 comes out of the synchronized block without creating another instance. Further calls to this method return from the first check only. By the way, double-checked locking idiom was broken before Java 5.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-demonstrate-the-double-check-locking-for-singleton-class
Java Program to Demonstrate the Double-Check Locking For Singleton Class - GeeksforGeeks
July 23, 2025 - 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.
Find elsewhere
🌐
Wikipedia
en.wikipedia.org › wiki › Double-checked_locking
Double-checked locking - Wikipedia
6 days ago - There are valid forms of the pattern, including the use of the volatile keyword in Java and explicit memory barriers in C++. The pattern is typically used to reduce locking overhead when implementing "lazy initialization" in a multi-threaded environment, especially as part of the Singleton pattern.
🌐
Medium
medium.com › @alxkm › java-double-check-locking-pattern-3f8782e9d01e
Java: Double check locking pattern | by Alex Klimenko | Medium
August 4, 2025 - Double-checked locking is a design pattern used to reduce the overhead of acquiring locks by first testing the locking condition without holding the lock. If the condition passes, the lock is acquired, and the condition is checked again within ...
🌐
Java Design Patterns
java-design-patterns.com › patterns › double-checked-locking
Double-Checked Locking Pattern in Java: Ensuring Thread Safety with Minimal Overhead | Java Design Patterns
In Java, it can be redundant or broken in some versions unless volatile variables are used with care. Singleton: Double-Checked Locking is often used in implementing thread-safe Singletons.
🌐
CodeSignal
codesignal.com › learn › courses › java-concurrency-foundations › lessons › optimizing-synchronization-with-double-checked-locking
Optimizing Synchronization with Double-Checked Locking
These concepts will help you build efficient, thread-safe applications that can manage shared resources effectively. The Problem with Basic Synchronization in a Singleton · The Singleton pattern ensures that only one instance of a class is created throughout the lifecycle of an application.
🌐
DEV Community
dev.to › devcorner › thread-safe-singleton-in-java-understanding-volatile-and-double-checked-locking-3d1a
Thread-Safe Singleton in Java: Understanding `volatile` and Double-Checked Locking - DEV Community
March 5, 2025 - The Singleton pattern is widely used in Java but needs proper thread safety. Double-checked locking with volatile is the most efficient way to implement a thread-safe singleton.
🌐
Medium
medium.com › javarevisited › how-to-initialize-a-singleton-with-double-checked-locking-in-java-dfa641e1d63b
How To Initialize A Singleton With Double-Checked Locking in Java | by Sumant | Javarevisited | Medium
April 30, 2021 - Just taking a lock on the class while creating the object doesn’t work. Suppose thread A and B both enter the if-block and let's say thread A gets a lock and creates a new object. After it exits, thread B takes a lock and creates another object. This is where double-checked lock comes into play.
🌐
YouTube
youtube.com › watch
Singleton and Double Checked Locking - YouTube
Implementing singleton pattern in Java using idiom of double checked locking, static holder pattern and enums.Channel----------------------------------Master...
Published   February 24, 2019
🌐
javathinking
javathinking.com › blog › double-checked-locking-in-singleton
Double Checked Locking in Singleton: Implementing Thread-Safe Java Singleton with Custom Class Example — javathinking.com
Thread B checks instance == null ... → Two instances exist! Double Checked Locking (DCL) is an optimization to make Singletons thread-safe and performant....
🌐
SEI CERT
wiki.sei.cmu.edu › confluence › display › java › LCK10-J.+Use+a+correct+form+of+the+double-checked+locking+idiom
LCK10-J. Use a correct form of the double-checked locking idiom - SEI CERT Oracle Coding Standard for Java - Confluence
The double-checked locking pattern uses block synchronization rather than method synchronization and installs an additional null reference check before attempting synchronization.
🌐
Blogger
camelcase.blogspot.com › 2006 › 11 › singleton-pattern-and-double-checked.html
CamelCase: Singleton pattern and Double-Checked Locking problem
In this article, Peter Haggar examines the roots of the double-checked locking idiom, why it was developed, and why it doesn't work. http://www-128.ibm.com/developerworks/java/library/j-dcl.html I think the problem with DCL comes from checking the wrong variable. Instead of checking the static instance itself one should check a boolean, which is by nature an atomic operation. This would prevent the mentioned JVM memory model "problem". What do you think? public class Singleton { private static Singleton _instance = null; private static boolean _initialized = false; private Singleton() { } public static Singleton getInstance() { if (!_initialized) { synchronized (Singleton.class) { if (!_initialized) { _instance = new Singleton(); _initialized = true; } } } return _instance; } }
🌐
Medium
medium.com › @pvprasanth474 › singleton-pattern-with-double-checked-locking-for-improved-performance-in-java-7082f284e587
Singleton Pattern with Double-Checked Locking for Improved Performance in Java | by PV Prasanth | Medium
January 21, 2025 - Thread-Safe: Ensures that only one instance of the Singleton is created, even in a multi-threaded environment. Improved Performance: Double-Checked Locking minimizes synchronization overhead.
🌐
CodingTechRoom
codingtechroom.com › tutorial › java-java-singleton-double-checked-locking
Java Singleton Double Checked Locking: A Comprehensive Guide - CodingTechRoom
In this tutorial, we’ll explore the Double Checked Locking pattern for implementing the Singleton design pattern in Java. This pattern ensures that a class has only one instance while minimizing the overhead associated with acquiring a lock each time the instance is requested.