I looked for creating an immutable data type with final fields (including an array that was constructed and populated before assigning the element to the final field), and noticed that the JVM seems to be specified to ensure that any other thread that gets a reference to this object will see the initialized fields and values โโof the array (if the pointer to this not indicated in the constructor, see What is a "partially constructed object"? and How do hidden JVM memory barriers behave when creating a chain of constructors? ).
I am curious how this is achieved without synchronizing each access to this object or in any other way a significant decrease in performance is paid. In my opinion, the JVM can achieve this by doing the following:
- Print a record at the end of the constructor
- Publish a link to a new object only after recording
- Perform a read-pick anytime you refer to the final field of an object
I cannot come up with a simpler or cheaper way to eliminate the risk of other threads considering uninitialized leaf fields (or recursive links through leaf fields).
It seems like this can impose a severe penalty for performance because of all the reading barriers in other threads reading the object, but the elimination of capitalization leads to the fact that the reference to the object is examined in another processor before it issues a reading or viewing updates in places memory corresponding to the newly initialized end fields.
Does anyone know how this works? And does this mean a significant decrease in productivity?
java concurrency
jonderry
source share