Fundamentals 14 min read

Physical Memory Model, Concurrency Concepts, and Java Memory Model Explained

This article explains the modern computer physical memory hierarchy, introduces three key concurrency concepts—atomicity, ordering, and visibility—illustrates them with Java code examples, and then details the Java Memory Model and its eight happen‑before principles that govern multithreaded behavior.

Beike Product & Technology
Beike Product & Technology
Beike Product & Technology
Physical Memory Model, Concurrency Concepts, and Java Memory Model Explained

Modern computers typically have multiple CPU cores, each accessing a hierarchical memory system that includes registers, multiple levels of cache, and main memory. The logical view of memory appears as a single address space, while caches and registers bridge the speed gap between CPU and RAM.

The speed relationship is register > L1 cache > L2 cache > ... > n‑level cache > memory , and the capacity relationship is memory > n‑level cache > ... > L2 cache > L1 cache > register . Caches improve performance because CPUs frequently reuse recently accessed memory addresses.

Three fundamental concurrency concepts arise from this memory model:

Atomicity : an operation either completes fully or not at all. For example, a = a + 1; is broken into three atomic steps: load, compute, store.

Ordering : the execution order of operations matters. Instruction reordering can change the perceived order unless prevented by synchronization.

Visibility : changes made by one thread must become visible to other threads, which requires proper memory barriers.

Java implements these concepts through the Java Memory Model (JMM). The JMM separates storage into thread stacks and a shared heap, defining where member variables, local primitives, and object references reside.

JMM supports eight happen‑before guarantees that ensure correct multithreaded behavior:

Single‑thread program order.

Unlock of a lock happens‑before subsequent lock acquisition of the same lock.

Write to a volatile variable happens‑before any read of that variable.

Transitivity of happen‑before relations.

Thread start happens‑before any actions in the started thread.

Thread interrupt happens‑before the interrupted thread detects the interrupt.

Thread termination happens‑before any other thread detects that the thread has finished.

Object construction happens‑before its finalize method.

These principles are illustrated with Java code examples, showing how instruction reordering, lock acquisition, and volatile writes affect program outcomes. Understanding and applying these eight rules is essential for mastering Java concurrency beyond merely using high‑level concurrent libraries.

concurrencymultithreadingMemory ModelCPU cacheHappen-Before
Beike Product & Technology
Written by

Beike Product & Technology

As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.