的安全初始化從Java theory and practice: Fixing the Java Memory Model, Part 2非final字段
The new JMM also seeks to provide a new guarantee of initialization safety -- that as long as an object is properly constructed (meaning that a reference to the object is not published before the constructor has completed), then all threads will see the values for its final fields that were set in its constructor, regardless of whether or not synchronization is used to pass the reference from one thread to another.
那麼,什麼是關於構造函數初始化非最終字段?從a reference to the object is not published before the constructor has completed
我可以得出結論,行爲與最終字段相同。這是對的嗎?
如果'Example'也有volatile變量'c'會怎麼樣?這個變量初始化的行爲如何? –
@michaelnesterenko在'c'的構造函數中設置的值將從其他線程可見。你也可以用它來安全地發佈'j',但是這很容易出錯。 – assylias