☕ Java Advanced

What is the Java Memory Model (JMM)?

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.