Java Reference
In-Depth Information
• If hb(x, y) and hb(y, z) , then hb(x, z) .
The wait methods of class Object 17.2.1 ) have lock and unlock actions associated with
them; their happens-before relationships are defined by these associated actions.
It should be noted that the presence of a happens-before relationship between two actions
does not necessarily imply that they have to take place in that order in an implementation.
If the reordering produces results consistent with a legal execution, it is not illegal.
For example, the write of a default value to every field of an object constructed by a
thread need not happen before the beginning of that thread, as long as no read ever
observes that fact.
More specifically, if two actions share a happens-before relationship, they do not necessar-
ily have to appear to have happened in that order to any code with which they do not share
a happens-before relationship. Writes in one thread that are in a data race with reads in an-
other thread may, for example, appear to occur out of order to those reads.
The happens-before relation defines when data races take place.
A set of synchronization edges, S , is sufficient if it is the minimal set such that the transitive
closure of S with the program order determines all of the happens-before edges in the exe-
cution. This set is unique.
It follows from the above definitions that:
• An unlock on a monitor happens-before every subsequent lock on that monitor.
• A write to a volatile field (§ 8.3.1.4 ) 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.
When a program contains two conflicting accesses (§ 17.4.1 ) that are not ordered by a
happens-before relationship, it is said to contain a data race .
The semantics of operations other than inter-thread actions, such as reads of array lengths
10.7 ) , executions of checked casts (§ 5.5 , § 15.16 ), and invocations of virtual methods
15.12 ) , are not directly affected by data races.
Search WWH ::




Custom Search