Happens-before rules
Program Order rules. if action A in the program is before action B, then a operation in the thread will be executed before the B operation.
Monitor lock rules. the unlock operation on the monitor lock must be performed before the lock operation on the same monitor lock.
volatile variable rules. a write operation on a volatile variable must be performed before the read operation on the variable.
thread start rule. the call to Thread.Start on the thread must be executed before any operation is performed on the threads.
thread end rule. any action in the thread must be executed before another thread detects that the thread has ended, or it returns successfully from Thread.Join, or False when the thread.isalive is called.
The interrupt rule. when a thread calls interrupt on another thread, it must be executed before the interrupt call is detected by the interrupted thread (by throwing interruptedexception, or call isinterrupted and interrupted).
The finalizer rule. the constructor of the object must be completed before the finalizer of the object is started.
transitivity. If operation A is performed before action B and action B is performed before Operation C, then operation a must be performed before Operation C.
The following is an English description of happens-before,the Happens-before relation defines when data races take place.
- An unlock in a monitor happens-before every subsequent lock on the that monitor.
- A write to a volatile field happens-before every subsequent read of the that field.
- A call to start () on a thread happens-before any of the actions in the started thread.
- All the actions in a thread happen-before any and thread successfully returns from a join () to that thread.
- The default initialization of any object Happens-before any other actions (other than default-writes) of a program.
Java Concurrency Programming Practice Note-happens-before rules