Java memory model - volatile and x86
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
The Java Memory Model (JMM) is a crucial component of the Java programming language that defines how threads interact through memory. It specifies the rules by which memory operations (such as reads and writes to variables) interact in multi-threaded environments. Key elements of the JMM include the `volatile` keyword and considerations for the x86 architecture. This article will explore these topics in detail.
The Java Memory Model
The JMM provides guidelines that ensure consistent and predictable behavior of Java applications in a multi-threading context. It defines:
- Visibility: When a thread updates a variable, other threads should see the updated value.
- Atomicity: Certain operations (like reading and writing `volatile` variables) appear to be indivisible.
- Ordering: How different threads perceive the sequence of operations.
`volatile` Keyword
The `volatile` keyword in Java is a modifier applicable to variables. Declaring a variable as `volatile` establishes a happens-before relationship, ensuring visibility of changes across threads.
Characteristics of Volatile Variables
- Visibility: Reads and writes to a `volatile` variable establish a clear memory barrier. A write to a `volatile` field in one thread makes the value immediately visible to other threads.
- Ordering: Volatile variables prevent instruction reordering by the compiler and the CPU. Though they don't guarantee atomicity of compound actions, they prevent sequence rearrangement.
- Non-Caching: Java threads are prohibited from caching `volatile` variables.
Example of Volatile Usage
Here's a common example with a `volatile` flag used to stop a thread:
- Memory Ordering: x86 memory models are generally strong, maintaining a total store order. They inherently support the visibility and ordering guarantees the JMM requires.
- Cache Coherency: x86 processors support coherent caches, minimizing the need for software-level visibility management like `volatile`.
- Instruction Reordering: While x86 allows some reordering, `volatile` in Java sends memory fence instructions that ensure proper order, consistent with JMM guarantees.
Related reading
- Java multi-threading Safe Publication
- Java Multithreading for IVRS with GSM Modem rxtx playing voice file making event listener stop working
- Java Non-Blocking and Asynchronous IO with NIO NIO.2 JSR203 - Reactor/Proactor Implementations
- Java notify() vs. notifyAll() all over again
- Java merge 2 collections in O1
- Java method with return type compiles without return statement
- Java notify vs. notifyAll all over again
- Java .parallelStream with spring annotated methods

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.