Back to Blog
javaconcurrencyvolatilesynchronized

Java Volatile vs Synchronized: Visibility and Performance Trade-offs

Master memory visibility. Learn when volatile is enough and when you need synchronization. Understand happens-before relationships.

J

JOptimize Team

June 14, 2026· 11 min read

Visibility problems are subtle. Two threads can see different values of same variable without synchronization.

Volatile: ensures reads always get fresh value. Cost ~10ns. Use for simple flags (shutdown, ready).

Synchronized: ensures atomicity + visibility. Cost ~100ns uncontended. Use for compound operations (check+update).

Happens-before relationship: volatile write happens-before volatile read on different threads.

When volatile is enough: single boolean flag, reference to immutable object, visibility only needed.

When synchronized is needed: compound operations like i++, protecting mutable state, atomicity matters.

Alternative: AtomicReference provides volatile semantics without lock overhead.

Production checklist: Use volatile for simple flags, synchronized for compound ops, AtomicXxx for lock-free updates.

Summary: Volatile ensures visibility (10ns). Synchronized ensures atomicity (100ns+). Choose based on need.

Optimize with JOptimize PRO. Use code LINKEDIN40 for 40% OFF.

Want to go deeper?

Master Spring Boot, security, and Java performance with hands-on courses.

Detect issues in your project

JOptimize finds N+1 queries, EAGER collections, and 70+ other issues in your Java codebase — in under 30 seconds.