☕ Java Advanced

What is the Java Memory Model (JMM)?

Why Interviewers Ask This

This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.

Answer

The Java Memory Model (JMM) defines the rules for how threads interact through memory — specifically, when writes made by one thread become visible to other threads. By default, threads may cache variable values locally and not immediately flush to main memory. The JMM establishes happens-before relationships: if action A happens-before action B, then A's memory effects are visible to B. Happens-before is established by: thread start (all writes before start() are visible to the started thread), locking/unlocking the same monitor, volatile reads/writes, and joining a thread. Understanding the JMM is critical for writing correct concurrent code without data races.

Common Mistake

Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real Java project.