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 โ€บ 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.
๐ŸŒ
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....
Find elsewhere
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Double-checked_locking
Double-checked locking - Wikipedia
2 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.
๐ŸŒ
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....
๐ŸŒ
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
๐ŸŒ
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 โ€บ @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.
๐ŸŒ
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.
Top answer
1 of 3
3

Illustrating example of a singleton with respect to double-checked locking that looks clever but is broken

The start of a synchronization block guarantees that you see the latest data, but it does not guarantee reordering, you cannot expect a consistent view of data unless you are also in a synchronized block. It doesn't guarantee, that variables modifications done within synchronized section will be visible to other threads. Only the threads that enters the synchronized block is guaranteed to see the changes. This is the reason why double checked locking is broken - it is not synchronized on the reader's side. The reading thread may see, that the singleton is not null, but singleton data may not be fully initialized (visible).

On the other hand, ordering is provided by volatile which guarantees ordering, for instance write to volatile singleton static field guarantees that writes to the singleton object will be finished before the write to a volatile static field. It doesn't prevent creation singleton of two objects; this is provided by synchronize. Class final static fields doesn't need to be volatile. In Java, the JVM takes care of this problem.

More can be found in:

  • Double-checked locking: Clever, but broken
  • The "Double-Checked Locking is Broken" Declaration
2 of 3
2

It would be difficult for someone to be sure that their application had actually been hit by a double-checked lock failure. Indeed, many applications that used this idiom may never experience the problem for a variety of reasons.

However, that doesn't mean that you should use it. The mere fact there is a non-quantifiable probability of failure should be sufficient to persuade you not to use double-checked locking, especially since there are safe alternatives.

You've just been lucky.

๐ŸŒ
Code Pumpkin
codepumpkin.com โ€บ home โ€บ singleton design pattern using double checked locking
Singleton Design Pattern Using Double Checked Locking | Code Pumpkin
October 22, 2018 - They don't have to wait for lock on synchronized block. Is it thread-safe? Can you think of any situation which can result into creation of multiple instances? Here is the case in which singleton design pattern will break with this approach : This will bring us to double checked locking pattern, where we will also add one more null check inside synchronized block as shown below.