fundamentals
What are happens-before relationships in JMM?
The Happens-Before principle is a formal guarantee that memory writes by one specific statement are visible to another specific statement.
Key Rules:
- Program Order Rule: Actions in a single thread happen according to code order.
- Monitor Lock Rule: An unlock on a monitor happens-before every subsequent lock on that same monitor.
- Volatile Variable Rule: A write to a volatile field happens-before every subsequent read of that same field.
- Thread Start Rule: A call to
Thread.start()happens-before any action in the started thread. - Thread Termination Rule: Any action in a thread happens-before any other thread detects that thread has terminated.