[concurrency-interest] Visibility of array initialization

Raph Frank raphfrk at gmail.com
Sun Aug 12 10:29:20 EDT 2018


I was wondering what are the visibility rules for array initialization.

If an array is initialized and then another thread gets access to a
reference to that array, are the elements guaranteed to be at least their
initial state (or could the array be all zeros)?


*Thread 1*
int[] array = new int[] {-1, -1, -1};
thread2.start();

......

array = new int[] {1, 2, 3};

*Thread 2*
System.out.println(array[2]);

Is it possible for the 2nd thread to print zero?  Is it guaranteed to print
-1 or 2?

The spec refers to final fields in the constructor of objects and also says
that arrays referenced by final fields are visible (including elements).

" It will also see versions of any object or array referenced by those final
fields that are at least as up-to-date as the final fields are. "

It doesn't give any indication of what happens when initializing raw
arrays.  Does the array initialization count as a constructor?

Is the logical process that arrays are set to all zeros at time zero and
then they are initialized when created?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180812/dfc942e3/attachment.html>


More information about the Concurrency-interest mailing list