By reading Java Concurrency in Practice
I can see:
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.
However, I am confused about the second idiom. Since volatile
can only guarantee that the reference is visible to another thread but it doesn't have synchronization of object construction which it refers to. So how can it guarantee that the mutable object is properly constructed, what is the thread that is constructing this object is interrupted by another thread?