[concurrency-interest] Visibility of array initialization

Yuval Shavit yankee.sierra at gmail.com
Sun Aug 12 14:21:01 EDT 2018


I believe the value will be -1 or 2, but not 0 -- not for any special array
semantics, but because the call to Thread::start introduces a
happens-before (JLS 17.4.5).

On Sun, Aug 12, 2018 at 1:48 PM Justin Sampson via Concurrency-interest <
concurrency-interest at cs.oswego.edu> wrote:

> The spec explicitly calls out the *length* of an array as being a final
> field, so it's always guaranteed to be seen correctly:
>
>
>
> https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.7
>
> https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.5
>
>
>
> But array elements are just regular heap variables, initialized to 0
> before the initializer expression is evaluated:
>
>
>
> https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.6
>
> https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1
>
>
>
> So yeah, I'm pretty sure that means you could see 0's if the array isn't
> published safely.
>
>
>
> Cheers,
>
> Justin
>
>
>
>
>
> *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};
>
> 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?
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180812/89a0c6e3/attachment-0001.html>


More information about the Concurrency-interest mailing list