...
Even if statements execute in program the order the appear in a thread, caching can prevent the latest values from being reflected in the main memory (visibility hazard). Program order is the execution order that is expected when a single thread is running the statements sequentially, as written in a method.
The Java Language Specification defines the Java Memory Model (JMM) which provides certain guarantees to the Java programmer. The JMM is specified in terms of actions, which includes variable reads and writes, monitor locks and unlocks, and thread starts and joins. The JMM defines a partial ordering called happens-before on all actions within the program. To guarantee that a thread executing action B can see the results of action A, for example, there must be a happens-before relationship defined such that A happens-before B.
...
- An unlock on a monitor happens-before every subsequent lock on that monitor.
- A write to a volatile field happens-before every subsequent read of that field.
- A call to
start()on a thread happens-before any actions in the started thread.- All actions in a thread happen-before any other thread successfully returns from a
join()on that thread.- The default initialization of any object happens-before any other actions (other than default-writes) of a program.
- A thread calling interrupt on another thread happens-before
the interrupted thread detects the interrupt- The end of a constructor for an object happens-before the
start of the finalizer for that object
| Wiki Markup |
|---|
If a happens-before relationship does not exist between two operations, the JVM is free to reorder them. A data race occurs when a variable is readwritten to by moreat thanleast one thread, and writtenread toby bya at least one other thread, and the reads and writes are not ordered by a happens-before relationship. A correctly synchronized program is one with no data races. The Java Memory Mode guarantees _sequential consistency_ for correctly synchronized programs. Sequential consistency means that the result of any execution is the same as if the (readreads and write) operationswrites by all processesthreads on theshared data store were executed in some sequential order and the operations of each individual processthread appear in this sequence in the order specified by its program \[[Tanenbaum 03|AA. Java References#Tanenbaum 03]\]. In other words: |
- Take the read /and write operations performed by each thread and put them in the order the thread executes them (thread order)
- Interleave the operations in some way allowed by the happens-before relationships to form a total program an execution order
- Read operations must return most recently written data in the total program order for the execution to be sequentially consistent
- Implies all processes threads see the same total ordering of the operationsof reads and writes of shared variables
| Wiki Markup |
|---|
The actual execution order of instructions and memory accesses can be in any order as long as the actions of the thread appear to that thread as if program order were followed, and provided all values read are allowed for by the memory model. This allows the programmer to understand the semantics of the programs they write, and it allows compiler writers and virtual machine implementors to perform various optimizations \[[JPL 06|AA. Java References#JPL 06]\]. |
...