Memory Model
Defines the necessary and sufficient conditions for knowing what writes to memory by other processors are visible to the currentt processor, and vice versa
JMM
Sequential vs Multithreaded Environments
Single
* The Java language specification requires the JVM to maintain within-thread as-if-serial semantics
* As long as the program has the same result as if it were executed in program order in a strictly sequential environment, out-of-order executions are permissible
Multi-threadd
* The illusions of sequantiality cannot be maintained without significant performance cose; i.e. a strong memory model, which ensures that all processors see exactly the same value for a given memory location at all times, is too expensive
* with a weaker memory model, a thrad might not see the most up-to-date value for a variable and memory actions in other threads can appear to happen out of order
* memory barriers are required to flush or invalidate the local processor cache to see writes made by other processors or make writes by this processor visible to others
Happens before rules
Program order rule
that each action in a thread, happens before every action in that thread that
comes later in the program order
Thread start rule
A call to Thread.start on a thread happens-before every action in the started thread
Thread termination rule
Any action in a thread happens-before any other thread detects that thread has terminated, either by successfully returning from Thread.join or by Thread.isAlive resturning false
Volatile variable rule
A write to a volatile filed happens before every subsequent read of that same field
Finaliser rule
The end of a constructor for an object happens before the start of the finaliser for that object
Final fields
Static initialiser