Java FAQ: Top Questions
44. What is the Java Memory Model (JMM)?
The Java Memory Model (JMM) defines how threads interact with memory, ensuring consistent behavior in concurrent programs, specified in the Java Language Specification.
-
Happens-Before:
Guarantees visibility of memory operations (e.g.,
volatile
,synchronized
). - Thread-Local Memory: Each thread may cache variables; JMM ensures synchronization.
-
Visibility:
Ensures changes by one thread are visible to others (e.g., via
volatile
). -
Atomicity:
Defines which operations are indivisible (e.g.,
long
writes may not be atomic). - Use Case: Writing correct, thread-safe code.