Java Reference
In-Depth Information
You may be surprised at how badly this harmless-looking example could fail. Because of vis-
ibility problems, the
Holder
could appear to another thread to be in an inconsistent state,
even though its invariants were properly established by its constructor! This improper pub-
lication could allow another thread to observe a
partially constructed object
.
3.5.1. Improper Publication: When Good Objects Go Bad
You cannot rely on the integrity of partially constructed objects. An observing thread could
see the object in an inconsistent state, and then later see its state suddenly change, even
published using the unsafe publication idiom in
Listing 3.14
, and a thread other than the pub-
Listing 3.15. Class at Risk of Failure if Not Properly Published.