How Java’s Memory Model Enables Thread Communication and Synchronization
The article explains Java’s concurrency mechanisms, detailing how threads communicate and synchronize via shared memory and the Java Memory Model (JMM), describing atomic actions, the MESI cache protocol, and key principles such as happens‑before, visibility, atomicity, and ordering.
Thread Communication and Synchronization
In concurrent programming two fundamental concerns are how threads exchange data (communication) and how they enforce a deterministic execution order (synchronization). Communication can be achieved via shared memory or message passing; synchronization is typically implemented with locks, volatile variables, or other ordering primitives.
Java Memory Model (JMM)
Java uses a shared‑memory model. Class (static) fields and object fields reside in the heap and are visible to all threads, while method parameters and local variables are stored on each thread’s stack and are thread‑private. The JMM defines the interaction between a thread’s local working memory and the main memory, and it relies on the MESI cache‑coherence protocol to keep caches consistent.
Typical communication between two threads proceeds as follows:
Thread A writes a modified variable to main memory.
Thread B reads the variable from main memory into its own working memory.
JMM Primitive Operations
lock : Marks a main‑memory variable as exclusively owned by a thread.
unlock : Releases a previously locked variable, allowing other threads to acquire it.
read : Transfers a variable’s value from main memory to the thread’s working memory.
load : Copies the value obtained by read into the working‑memory replica.
use : Passes a working‑memory value to the execution engine (CPU).
assign : Stores a value returned by the execution engine into the working‑memory variable.
store : Sends a working‑memory value back to main memory.
write : Completes the transfer from working memory to main memory.
MESI Cache‑Coherence Protocol
The MESI protocol ensures that when a CPU writes to a shared cache line, other CPUs invalidate their copies, forcing subsequent reads to obtain the latest value from main memory.
M (Modified) : Cache line contains data that differs from main memory and is exclusive to this cache.
E (Exclusive) : Cache line matches main memory and is held exclusively.
S (Shared) : Cache line matches main memory and may be present in multiple caches.
I (Invalid) : Cache line is not valid.
Three Core Concurrency Properties
Atomicity : An operation executes indivisibly, without interleaving from other threads.
Visibility : Changes made by one thread to a shared variable become observable by other threads.
Ordering : The relative execution order of actions is well defined.
JMM Guarantees and Happens‑Before Semantics
As‑if‑serial semantics : Regardless of compiler or processor reordering, the program’s observable result must be identical to a serial execution.
Happens‑before principle : If operation A happens‑before operation B, then the effects of A are guaranteed to be visible to B.
Program order rule: Within a single thread, earlier actions happen‑before later actions.
Volatile rule: A write to a volatile variable happens‑before any subsequent read of that variable.
Transitivity rule: If A → B and B → C, then A → C.
Lock rule : Unlocking a monitor happens‑before the next lock acquisition of the same monitor.
Thread lifecycle rules :
Calling Thread.start() on thread B happens‑before any action in thread B.
Calling Thread.join() on thread B causes the joining thread to see all effects of thread B after it terminates.
Calling Thread.interrupt() happens‑before the interrupted thread detects the interrupt status.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Thoughts on Knowledge and Action
Travel together, with knowledge and action all the way
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
