Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Understanding Double-Checked Locking Singleton Pattern in Java
Fundamentals 5 min read

Understanding Double-Checked Locking Singleton Pattern in Java

This article explains the double-checked locking implementation of the Singleton pattern in Java, detailing why two null checks are necessary, the role of the volatile keyword in preventing instruction reordering and ensuring visibility across threads, and provides a complete example code.

The article begins with a complete Java implementation of a thread‑safe Singleton using double‑checked locking, including the class definition, a volatile static instance field, a private constructor, and a synchronized block inside the getInstance method.

package cn.yqh.interview;

public class Singleton {
    private static volatile Singleton singleton = null;
    private Singleton() {}
    public static Singleton getInstance() {
        // first check
        if (singleton == null) {
            synchronized (Singleton.class) {
                // second check
                if (singleton == null) {
                    singleton = new Singleton();
                }
            }
        }
        return singleton;
    }
    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            new Thread(new Runnable() {
                public void run() {
                    System.out.println(Thread.currentThread().getName() + " : " + Singleton.getInstance().hashCode());
                }
            }).start();
        }
    }
}

The first null check (the "first verification") avoids entering the synchronized block once the instance has been created, improving performance by eliminating unnecessary lock contention.

The second null check (the "second verification") prevents multiple threads from creating separate instances when they concurrently pass the first check; without it, both threads could instantiate the Singleton, breaking the pattern.

The article also emphasizes that the volatile modifier on the instance field is essential. It stops the JVM from reordering the three steps of object creation (memory allocation, initialization, and reference assignment), which could otherwise allow another thread to see a partially constructed object.

Furthermore, volatile guarantees visibility: changes to the variable are always read from main memory, ensuring that all threads observe the most recent value.

In summary, both null checks and the volatile keyword are indispensable for a correct, thread‑safe Singleton implementation using double‑checked locking.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaConcurrencyvolatilesingletondouble-checked locking

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion