Fundamentals 5 min read

Why Double-Checked Locking Fails in Java and How Volatile Fixes It

This article explains the instruction‑reordering problem that breaks the double‑checked locking singleton pattern in Java, shows how the volatile keyword prevents reordering and ensures memory visibility, and compares volatile with synchronized for thread safety.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why Double-Checked Locking Fails in Java and How Volatile Fixes It

In Java multithreading, the classic double‑checked locking singleton implementation can be broken because the JVM may reorder instructions during optimization, causing the instance reference to be assigned before the object is fully initialized.

The fix is to declare the volatile instance field, which forbids such reordering and guarantees that any thread reading the field sees a fully constructed object.

Instruction reordering is an optimization performed by the compiler, JVM, and CPU to increase parallelism while preserving the results of single‑threaded execution. In a multithreaded context, this can lead to unexpected behavior, such as a thread observing a partially constructed singleton.

Besides preventing reordering, volatile also provides memory visibility: writes to a volatile variable are immediately visible to other threads, eliminating the need for explicit synchronization for simple flag or reference updates.

Compared with synchronized , volatile is a lightweight synchronization mechanism that cannot block threads, works only on variables, and does not guarantee atomicity of compound actions. synchronized can protect critical sections, ensure both visibility and atomicity, and can be applied to methods or blocks.

Key differences:

volatile offers better performance but only guarantees visibility.

synchronized provides both visibility and atomicity, but may cause blocking.

volatile cannot be used to protect complex state changes.

Understanding these concepts is essential for writing correct concurrent Java code and avoiding subtle bugs caused by instruction reordering.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

multithreadingdouble-checked locking
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.