Java Reference
In-Depth Information
To preserve the integrity of the system, classes must use final fields ap-
propriately to protect sensitive data, if correct synchronization cannot
be relied on. For this reason, the String class uses final fields internally
so that String values are both immutable and guaranteed to be visible.
14.10.3. The Happens-Before Relationship
The description of synchronizations actions above is a simplification of
the operation of the memory model. The use of synchronized blocks and
methods and the use of volatile variables provide guarantees concerning
the reading and writing of variables beyond the volatile variables them-
selves or the variables accessed within the synchronized block. These
synchronization actions establish what is called a happens-before rela-
tionship and it is this relationship that determines what values can be re-
turned by a read of a variable. The happens-before relationship is trans-
itive, and a statement that occurs ahead of another in program order
happens-before that second statement. This allows actions other than
synchronization actions to become synchronized across threads. For ex-
ample, if a non-volatile variable is written before a volatile variable,
and in another thread the same volatile variable is read before read-
ing the non-volatile variable, then the write of the non-volatile variable
happens-before the read of that variable and so the read is guaranteed
to return the value written. Consider the following:
Search WWH ::




Custom Search