The memory model determines what values can be read at every point in the program. The actions of each thread in isolation must behave as governed by the semantics of that thread, with the exception that
the values seen by each read are determined by the memory model. When we refer to this, we say that the program obeys intra-thread semantics.
Intra-thread semantics are the semantics for singlethreaded programs, and allow the complete prediction of the behavior of a thread based on the values seen by read actions within the thread. To determine if the actions of
thread t in an execution are legal, we simply evaluate the implementation of thread t as it would be performed in a single-threaded context, as defined in the rest of this specification.(Java
Language Specification)
Inter-thread action(线程间语义):
An inter-thread action is an action performed by one thread that can be detected or
directly influenced by another thread. There are several kinds of inter-thread action
that a program may perform:
? Read (normal, or non-volatile). Reading a variable.
? Write (normal, or non-volatile). Writing a variable.
? Synchronization actions, which are:
– Volatile read. A volatile read of a variable.
– Volatile write. A volatile write of a variable.
– Lock. Locking a monitor
– Unlock. Unlocking a monitor.
– The (synthetic) first and last action of a thread.
– Actions that start a thread or detect that a thread has terminated (§17.4.4).
? External Actions. An external action is an action that may be observable outside
of an execution, and has a result based on an environment external to the
execution.
? Thread divergence actions (§17.4.9). A thread divergence action is only
performed by a thread that is in an infinite loop in which no memory,
synchronization, or external actions are performed. If a thread performs a thread
divergence action, it will be followed by an infinite number of thread divergence
actions.
Program Order(程序顺序):
Among all the inter-thread actions performed by each thread t, the program orderof
t is a total order that reflects the order in which these actions would be performedaccording to the intra-thread semantics of t.(Java
Language Specification)
For execution of a single thread, the rules are simple. The Java
Language Specification requires a Java
Virtual Machine to observe within-thread as-if-serial semantics.
The runtime (which, in this case, usually refers to the dynamic compiler, the processor and the memory subsystem) is free to introduce any useful execution optimizations as long as the result of the thread in isolation is guaranteed to be exactly the same
as it would have been had all the statements been executed in the order the statements occurred in the program (also called program order).
Mathematically, there is a partial
order called the happens-before order
over all actions performed by the program. The happens-before order subsumes the program order; if one action occurs before another in the program order, it will occur before the other in the happens-before order. In addition, releases and subsequent acquires
of locks form edges in the happens-before graph. A read is allowed to return the value of a write if that write is the last write to that variable before the read along some path in the happens-before order, or if the write is not ordered with respect to that
read in the happens-before order.(http://en.wikipedia.org/wiki/Java_Memory_Model)