What is the Java Memory Model and the happens-before relationship?
Reviewed by Gurusankar M.
Short Answer
The JMM defines when one thread's writes become visible to another, allowing the compiler/CPU to reorder operations for speed. happens-before is the ordering guarantee: if A happens-before B, A's effects are visible to B. volatile, locks, thread start/join, and final fields establish it.
On this page
Concept Made Simple
Coffee Chat Question
“What is the Java Memory Model and the happens-before relationship?”
Remember It Faster
Mind Map Answer
- unlock → lock
- same monitor establishes h-b
- volatile write → read
- publishes prior writes
- Thread.start
- h-b everything in the new thread
- Thread.join
- joined thread's writes visible after
Think Beyond the Expected
What If?
Why can a correctly-looking program print stale or impossible values without synchronization?
Without a happens-before edge, the JIT/CPU may reorder writes and cache values in registers, so another thread sees stale or out-of-order data. Synchronization (volatile/locks) inserts the memory barriers that forbid those reorderings.
Real World
The JMM is why 'it works on my machine' concurrency bugs appear only under load or on different CPUs — reordering is legal until you establish happens-before with proper synchronization.
Interviewer's Expectation
Keywords they're listening for:
- reordering allowed
- happens-before edges
- volatile/locks/start/join/final
- visibility vs atomicity
- memory barriers
Common Mistakes
- Assuming sequential consistency without synchronization
- Relying on timing instead of happens-before
- Unsafe publication of partially-constructed objects
Best Practices
- Establish happens-before via volatile/locks for shared data
- Use final fields / immutability for safe publication
- Prefer java.util.concurrent over hand-rolled sync
Follow-up Questions
- How do final fields get safe-publication guarantees?
- What memory barriers does a volatile write insert?
- Why is double-checked locking broken without volatile?
References
Continue Learning with AI
Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly — AI is your learning companion, not a shortcut.
Plain-language foundations
I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner. Topic: volatile & Memory Model (Multithreading) Interview question: "What is the Java Memory Model and the happens-before relationship?" Please: 1. Explain the core idea in simple, plain language, using an everyday analogy. 2. Define any technical terms you use. 3. Walk through one small, concrete example. 4. Finish with a single sentence I can easily remember. Keep the tone friendly and assume I'm new to this topic.
Was this answer helpful?
Affiliate links
Tools for Your Developer Workspace
Equipment for a comfortable, productive setup.
As an Amazon Associate I earn from qualifying purchases.
- MonitorView on Amazon : BenQ EW2790U 27-inch Monitor (opens in a new tab)
BenQ EW2790U 27-inch Monitor
- LaptopView on Amazon : Acer Aspire One Laptop (opens in a new tab)
Acer Aspire One Laptop
- Office ChairView on Amazon : Wakefit Gravita Ergonomic Office Chair (opens in a new tab)
Wakefit Gravita Ergonomic Office Chair