"in Java if a call to a constructor has been inlined then the shared variable may immediately be updated once the storage has been allocated but before the inlined constructor initializes the object."
Could someone please help me with it.
Chan Ag wrote:Could someone please help me with it.
Inlining of code is when the compiler replaces a method call with the body of the code. This done to improve performance often at the expense of program size.
it is possible to envisage a scenario where rather than storing the reference to newly created but uninitialized object in a JVM local variable which is only assigned to the object variable on constructor completion, the in-lined code uses the object variable to store the reference as soon as it is created. Hence it can have a reference to a created but uninitialized or partly initialized object.
Basically, synchronization and volatility inhibit that process in certain ways, ensuring that, in terms of visibility, statements are ordered exactly the way you wrote them.
If you want more information, you might try Googling "happens-before".
Somewhere in the discussion, the authors also imply ( I'm not sure if my interpretation of what the authors mean is right here. Hence I say imply ) that it's possible that the second thread gets the reference of the object before the first thread is done constructing the object. The second thread can then call other methods that can be called on the constructed object ( obviously methods cannot be called if object was still null. Hence the authors must mean the reference the object would get once it is constructed is leaked before the construction is complete. Or am I missing something here? ) although the first thread is not yet done constructing the object. I could not understand how this is possible.
Chan Ag wrote:God it's so <not explicit> in some cases.