Here’s a short explanation of Java Memory Model (JMM):
• JMM defines how threads interact with memory in Java.
• It controls visibility, ordering, and atomicity of variables between threads.
Key Points:
1. Main Memory vs Thread Local Cache
o Each thread can cache variables.
o Changes in one thread may not be immediately visible to others.
2. Happens-Before Relationship
o A rule to guarantee visibility and ordering.
o Example: Unlocking a lock happens-before another thread acquires that lock.
3. volatile
o Ensures visibility: writes to a volatile variable are visible to all threads
immediately.
o Does not ensure atomicity (except for long/double).
4. synchronized / Locks
o Ensure mutual exclusion + establish happens-before.
5. Atomic Classes (java.util.concurrent.atomic)
o Provide lock-free thread-safe operations.