Java Memory Model Flashcards
What is a strong memory model?
Where all processors see exactly the same value for a given memory location at all times
What is a weak memory model?
Where 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.
When can a data race occur?
A data race occurs when a variable is read by more than one thread, and written by at least one thread, but the reads and writes are not ordered by happens-before .
Explain happens-before
The JMM defines a partial ordering called happens-before on all actions within the program. To guarantee that the thread executing action B can see the results of action A, there must be a happens-before relationship between A and B. In the absence of a happens before ordering between two operations, the JVM is free to reorder them as it pleases.
What is a correctly synchronised program
A correctly synchronised program is one with no data races; correctly synchronised
programs exhibit sequential consistency, meaning that all actions within the program appear to happen in a fixed, global order. See the Java Volatile Keywords Deck for more information.
What are the rules of JMM (Happens-before)
Program order rule Monitor lock rule Volatile Variable rule Thread Start rule Thread Termination rule Interruption rule Finalizer rule Transitivity rule