HardExperience level: 8-15 years1 min read

What is the Java Memory Model and the happens-before relationship?

Reviewed by Gurusankar M.

Asked inAmazonGoogleMicrosoft
#jmm#happens-before#reordering#visibility#memory model
Report issue

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

  1. How do final fields get safe-publication guarantees?
  2. What memory barriers does a volatile write insert?
  3. 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.
Open inChatGPTGeminiClaude

Was this answer helpful?

Affiliate links

Equipment for a comfortable, productive setup.

As an Amazon Associate I earn from qualifying purchases.