How the synchronized Keyword and Monitors Work
Fast recall
Quick revision
Core answer
Deep understanding of the JVM memory model and how mutual exclusion is implemented under the hood.
Thinking that synchronized only ensures atomicity. It also establishes happens-before relationships, flushing caches and ensuring visibility.
Mention that every Java object has a monitor (intrinsic lock), that static synchronized methods lock the Class object, and that synchronized blocks are re-entrant.
Interview-ready guide
Interview answer
synchronized does two things: mutual exclusion (one thread at a time) and memory visibility (writes become visible when a thread exits the block).
Monitor basics:
- Every Java object has an intrinsic monitor
- Instance methods sync on
this; static methods sync on theClassobject - Synchronized blocks let you choose the monitor explicitly — prefer blocks over method-level sync for finer control
Best practices:
- Use
private final Object lockinstead ofthis— external code can sync on your object, causing unexpected contention - Never synchronize on String literals — interned strings share a monitor across the entire JVM
Performance:
- Uncontended synchronization is nearly free — the JVM uses lightweight CAS-based locking (biased/lightweight locking)
- Cost becomes significant only under contention when multiple threads compete for the same monitor
I recommend synchronized for simple critical sections and ReentrantLock when you need tryLock(), interruptible acquisition, or multiple conditions.
How the synchronized Keyword and Monitors Work
Direct Answer
synchronized acquires the intrinsic lock (monitor) of an object. Only one thread can hold a monitor at a time. It guarantees mutual exclusion AND visibility — changes made inside a synchronized block are visible to all threads that subsequently acquire the same lock.
Deep Dive & Architecture
Synchronization is how Java serializes access to shared mutable state so that concurrent code stays correct.
It provides two guarantees: mutual exclusion (only one thread executes the protected section at a time) and memory visibility (writes made inside a synchronized block by one thread are visible to any thread that later acquires the same lock).
Without synchronization, you get race conditions (lost updates) and visibility bugs (a thread reads stale values because the CPU cache hasn't flushed). Java provides multiple tools: the synchronized keyword (intrinsic monitor locks), volatile (visibility only, no atomicity), java.util.concurrent.atomic (lock-free atomics), and the java.util.concurrent.locks package (ReentrantLock, StampedLock, ReadWriteLock). Synchronization is enforced through the Java Memory Model (JSR 133) via happens-before relationships.
Mentioning only mutual exclusion. Missing the visibility / memory-model angle entirely. Confusing volatile (visibility only) with synchronized (both)
Common Mistakes & Pitfalls
Thinking that synchronized only ensures atomicity. It also establishes happens-before relationships, flushing caches and ensuring visibility.
Check your understanding
Answer these without looking back at the article.
- What is synchronization in Java? Why is it needed, and what guarantees does it provide?
- What's the difference between a synchronized method and a synchronized block in Java? When should you use each?
- What are the differences between the synchronized keyword and ReentrantLock, and when should you choose each?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.