Java Reference
In-Depth Information
An additional effect of making a variable volatile is that reads and writes
are guaranteed to be atomic. This extends the basic atomicity guaran-
tee to cover long and double variables.
A few other synchronization actions help make multithreading work
nicely:
Starting a thread synchronizes with the first action performed by
that thread when it executes. This ensures that a newly started
thread sees any data that was initialized by the creating threadin-
cluding the thread's own fields.
The final action of a thread synchronizes with any action that de-
tects that the thread has terminatedsuch as calling isAlive or in-
voking join on that thread. This ensures, for example, that if you
join a thread you can see all data written by that thread before it
terminatedsuch as the results of its computation.
Interrupting a thread synchronizes with any other action that de-
termines that the thread has been interrupted, such as the thread
throwing InterruptedException or another thread invoking isInter-
rupted on the thread.
The write of the default value (zero, null, or false) to any field
synchronizes with the first action in any thread. This ensures that
even in incorrectly synchronized programs a thread will never see
arbitrary values in fieldseither a specific value written by some
thread will be seen or the default value of the field will be seen.
14.10.2. Final Fields and Security
We described in Section 2.2.3 on page 46 how final fields are used to
define immutable values; indeed you can use final fields to define im-
mutable objects. There is a common misconception that shared access
to immutable objects does not require any synchronization because the
state of the object never changes. This is a misconception in general be-
cause it relies on the assumption that a thread will be guaranteed to see
the initialized state of the immutable object, and that need not be the
 
Search WWH ::




Custom Search