From the book Java concurrency in practice :
To publish an object safely, both the reference to the object and the object's state must be made visible to other threads at the same time. A properly constructed object can be safely published by:
Initializing an object reference from a static initializer
Storing a reference to it into a volatile field or AtomicReference
Storing a reference to it into a final field of a properly constructed object
Storing a reference to it into a field that is properly guarded by a
lock.
My questions are :
- What are the the differences between bullet points 2 and 3 ? I am interested in the difference between
volatile
approach andfinal
approach in terms of safe publication of the object . - What does he mean by final field of a properly constructed object in point 3 ? Before starting the bulleted points authors already mentioned that they are talking about a properly constructed object (which I assume is not letting the
this
reference to escape ). But once again why did they mention about properly constructed objects ?