Java Reference
In-Depth Information
structor, as well as to any variables reachable through those fields, become “frozen” when
the constructor completes, and any thread that obtains a reference to that object is guaranteed
to see a value that is at least as up to date as the frozen value. Writes that initialize variables
reachable through final fields are not reordered with operations following the post-construc-
tion freeze.
Initialization safety means that SafeStates in Listing 16.8 could be safely published even
through unsafe lazy initialization or stashing a reference to a SafeStates in a public static field
with no synchronization, even though it uses no synchronization and relies on the non-thread-
safe HashSet.
Listing 16.8. Initialization Safety for Immutable Objects.
However, a number of small changes to SafeStates would take away its thread safety. If
states were not final, or if any method other than the constructor modified its contents,
initialization safety would not be strong enough to safely access SafeStates without syn-
chronization. If SafeStates had other nonfinal fields, other threads might still see incor-
rect values of those fields. And allowing the object to escape during construction invalidates
the initialization-safety guarantee.
Search WWH ::




Custom Search