[concurrency-interest] Visibility of array initialization

Justin Sampson jsampson at guidewire.com
Sun Aug 12 13:44:11 EDT 2018

The spec explicitly calls out the length of an array as being a final field, so it's always guaranteed to be seen correctly:


But array elements are just regular heap variables, initialized to 0 before the initializer expression is evaluated:


So yeah, I'm pretty sure that means you could see 0's if the array isn't published safely.


From: Concurrency-interest <concurrency-interest-bounces at cs.oswego.edu> on behalf of "concurrency-interest at cs.oswego.edu" <concurrency-interest at cs.oswego.edu>
Reply-To: Raph Frank <raphfrk at gmail.com>
Date: Sunday, August 12, 2018 at 7:29 AM
To: "concurrency-interest at cs.oswego.edu" <concurrency-interest at cs.oswego.edu>
Subject: [concurrency-interest] Visibility of array initialization

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};


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

Thread 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/8eaa907a/attachment.html>

More information about the Concurrency-interest mailing list