[concurrency-interest] do constructors ever involve threading under the covers?

Aleksey Shipilev aleksey.shipilev at oracle.com
Wed Oct 3 11:49:50 EDT 2012


On 10/03/2012 06:59 PM, Andy Nuss wrote:
> My assumption: I am never getting seeded values from somewhere else. 
> All seed values are passed into the constructor with argument containers.
> 
> My first question is:  why is it ok to assign the final reference to the
> newly created ArrayList before filling it with values?

Because JMM guarantees nobody would see the under-initialized list
stored in the final field before constructor ends.

> Second question is: how is data race for the newly created values in the
> ArrayList solved just by making ArrayList member variable final (rather
> than volatile)?

Where's the data race exactly? Once constructor is doing freeze action,
everything in the list stored in final field is visible.

> So I am getting from you guys that I just have to deep copy the argument
> containers into my own newly created containers, and make the reference
> to the new containers final, AND make sure that no values are ever added
> to the containers after the constructor finishes.  Is this correct?

I think we are still confused here. My point is *it is too late* for
reader to enforce proper visibility if writer had published something
unsafely. Nothing will save you at this point, nor final, nor volatile,
nor deep-copying.

And if writer had indeed published something safely, and the published
value never changes, you might as well store the reference to it.

-Aleksey.


More information about the Concurrency-interest mailing list