Java Reference
In-Depth Information
Figure 16.1. Interleaving Showing Reordering in PossibleReordering .
PossibleReordering is a trivial program, and it is still surprisingly tricky to enumerate
its possible results. Reordering at the memory level can make programs behave unexpectedly.
It is prohibitively difficult to reason about ordering in the absence of synchronization; it is
much easier to ensure that your program uses synchronization appropriately. Synchronization
inhibits the compiler, runtime, and hardware from reordering memory operations in ways that
would violate the visibility guarantees provided by the JMM. [1]
16.1.3. The Java Memory Model in 500 Words or Less
The Java Memory Model is specified in terms of actions , which include reads and writes to
variables, locks and unlocks of monitors, and starting and joining with threads. The JMM
defines a partial ordering [2] called happens-before on all actions within the program. To
guarantee that the thread executing action B can see the results of action A (whether or not A
and B occur in different threads), 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.
Search WWH ::




Custom Search