<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">All these responses miss the important aspect: the creation of the second array is not atomic, and it is not guaranteed that the assignment of a new reference to array to be observable after a write of 3 to array[2].<div class=""><br class=""></div><div class="">It is allowed that array = new int[3]; is observed first, and only then array[2]=3 is observed.</div><div class=""><br class=""></div><div class="">So, array[2] = 0 is observable.<br class=""><div class=""><br class=""></div><div class="">Alex<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 12 Aug 2018, at 20:54, Valentin Kovalenko via Concurrency-interest <<a href="mailto:concurrency-interest@cs.oswego.edu" class="">concurrency-interest@cs.oswego.edu</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class=""><div class="">Hi Raph,</div><div class=""><br class=""></div><div class="">JMM does not make any difference between <span class="gmail-Grammar gmail-gr_gramm gmail-gr_disable_anim_appear gmail-gr-alert gmail-gr_inline_cards gmail-multiReplace gmail-gr_ gmail-gr_45" id="gmail-45" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">read</span>/write actions from/to static/non-static fields and to array elements: see "17.4.1. Shared Variables" (<a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1</a>) which says "All instance fields, static fields, and array elements are stored in heap memory. In this chapter, we use the term variable to refer to both fields and array elements."</div><div class=""><br class=""></div><div class="">So when you think about <span class="gmail-Grammar gmail-gr_gramm gmail-gr_disable_anim_appear gmail-gr-alert gmail-gr_inline_cards gmail-multiReplace gmail-gr_ gmail-gr_261" id="gmail-261" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">read</span>/write actions from/to array elements, treat them the same way you treat <span class="gmail-Grammar gmail-gr_392 gmail-gr_gramm gmail-gr-alert gmail-gr_run_anim gmail-gr_inline_cards gmail-replaceWithoutSep gmail-gr_ gmail-only-ins" id="gmail-392" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">read</span>/write actions from/to other shared variables.</div><div class=""><br class=""></div><div class="">Having said that, the <span class="gmail-ins-del gmail-gr_452 gmail-gr-alert gmail-gr_spell gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-multiReplace gmail-gr_" id="gmail-452" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">anwser</span> to your question "Is it possible for the 2nd thread to print zero?  Is it guaranteed to print -1 or 2?" is: The second thread is allowed to print either -1 or 3 and nothing else (NPE is also not allowed).</div><div class=""><br class=""></div><div class="">Let's give letter names to some actions in executions of your program:</div><div class=""><br class=""></div><div class="">----------thread1</div><div class="">// array = null //_wa - an implicit write</div><div class="">///////////////////////of the default value,</div><div class="">///////////////////////which according to JMM</div><div class="">///////////////////////happens-before <span style="font-size: 12px;" class="">the first action</span></div><div class="">///////////////////////<span style="font-size: 12px;" class="">in every thread</span></div><div class=""><br class=""></div><div class="">// array[2] = 0 // _w0 - an implicit write</div><div class="">///////////////////////of the default value<br class=""></div><div class=""><br class=""></div><div class="">int[] array = new int[] { //wa</div><div class="">  -1, -1,</div><div class="">  -1 //w(-1)</div><div class="">};</div><div class="">thread2.start();</div><div class="">array = new int[] { //wa'</div><div class="">  1, 2,</div><div class="">  3 //w3</div><div class="">};</div><div class=""><br class=""></div><div class="">----------thread2</div><div class="">System.out.println(</div><div class="">  array[ //ra, read array reference</div><div class="">    2 //r</div><div class="">  ]</div><div class="">);<br class=""></div><div class=""><br class=""></div><div class=""><NPE> while dereferencing array is impossible.</div><div class="">  <span class="gmail-gr-alert gmail-gr_spell gmail-gr_run_anim gmail-gr_610 gmail-gr_inline_cards gmail-ContextualSpelling gmail-gr_" id="gmail-610" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">ra</span> can't return null because the corresponding implicit write _wa is not allowed to be observed by <span class="gmail-Grammar gmail-gr_gramm gmail-gr-alert gmail-gr_run_anim gmail-gr_inline_cards gmail-multiReplace gmail-gr_ gmail-gr_1456" id="gmail-1456" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">read</span> <span class="gmail-ins-del gmail-gr-alert gmail-gr_spell gmail-gr_1454 gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-multiReplace gmail-gr_" id="gmail-1454" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">ra</span>:</div><div class="">    HB(_wa, <span class="gmail-gr_938 gmail-ins-del gmail-gr-alert gmail-gr_spell gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-multiReplace gmail-gr_" id="gmail-938" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">wa</span>) and HB(<span class="gmail-ins-del gmail-gr_620 gmail-gr-alert gmail-gr_spell gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-multiReplace gmail-gr_" id="gmail-620" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">wa</span>, <span class="gmail-ins-del gmail-gr_633 gmail-gr-alert gmail-gr_spell gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-multiReplace gmail-gr_" id="gmail-633" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">ra</span>), thus <span class="gmail-ins-del gmail-gr-alert gmail-gr_1003 gmail-gr_spell gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-multiReplace gmail-gr_" id="gmail-1003" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">wa</span> prevents <span class="gmail-ins-del gmail-gr-alert gmail-gr_spell gmail-gr_run_anim gmail-gr_1030 gmail-gr_inline_cards gmail-ContextualSpelling gmail-gr_ gmail-multiReplace" id="gmail-1030" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">ra</span> from observing _wa.</div><div class=""><br class=""></div><div class=""><0> is not allowed to be observed.</div><div class="">  Because the corresponding implicit write _w0 is not allowed to be observed by <span class="gmail-Grammar gmail-gr_gramm gmail-gr-alert gmail-gr_run_anim gmail-gr_inline_cards gmail-multiReplace gmail-gr_ gmail-gr_1447" id="gmail-1447" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">read</span> r:</div><div class="">    HB(_w0, w(-1)) and HB(w(-1), r), thus w(-1) prevents r from observing _w0.</div><div class=""><br class=""></div><div class=""><-1> is allowed to be printed</div><div class="">  Because the corresponding write w(-1) is allowed to be observed by <span class="gmail-Grammar gmail-gr_gramm gmail-gr-alert gmail-gr_run_anim gmail-gr_inline_cards gmail-multiReplace gmail-gr_ gmail-gr_144" id="gmail-144" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">read</span> r:</div><div class="">    HB(w(-1), r) and there is no write <span class="gmail-gr-alert gmail-gr_spell gmail-gr_1467 gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-gr_" id="gmail-1467" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">wx</span> such that (HB(w(-1), <span class="gmail-gr_387 gmail-gr-alert gmail-gr_spell gmail-gr_run_anim gmail-ContextualSpelling gmail-gr_inline_cards gmail-gr_" id="gmail-387" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">wx</span>) and HB(wx, r)), i.e. there is no write wx preventing read r from observing write w(-1).</div><div class=""><br class=""></div><div class=""><3> is allowed to be printed</div><div class="">  Because the corresponding write w3 is allowed to be observed by <span class="gmail-Grammar gmail-gr_gramm gmail-gr-alert gmail-gr_run_anim gmail-gr_1550 gmail-gr_inline_cards gmail-multiReplace gmail-gr_" id="gmail-1550" style="display:inline;border-bottom:2px solid transparent;background-repeat:no-repeat;color:inherit;font-size:inherit">read</span> r:</div><div class="">    neither HB(w3, r) nor HB(r, w3).</div><div class=""><br class=""></div><div class=""><2> is not allowed to be printed</div><div class="">  Because no execution contains an action writing 2 in array[2]. I assume this was a typo and you meant <3>.</div></div><div class=""><br class=""></div><div class="">It's also worth mentioning that JMM sais "The semantics of operations other than inter-thread actions, such as reads of array lengths (§10.7), executions of checked casts (§5.5, §15.16), and invocations of virtual methods (§15.12), are not directly affected by data races. Therefore, a data race cannot cause incorrect behavior such as returning the wrong length for an array." So as Justin has already pointed out, you can treat array.length as a final field.</div><br clear="all" class=""><div class=""><div dir="ltr" class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><div style="margin: 0px; padding: 0px 0px 3px; font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 14px;" class=""><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:12.8px" class="">Regards,</span><br class=""></div></div><div style="margin: 0px; padding: 0px 0px 3px; font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 14px;" class=""><span style="font-family:arial,sans-serif;font-size:12.8px;color:rgb(34,34,34)" class="">Valentin</span></div><div style="margin: 0px; padding: 0px 0px 3px; font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 14px;" class=""><span style="font-family:arial,sans-serif;font-size:12.8px;color:rgb(34,34,34)" class=""><a href="https://www.linkedin.com/in/stIncMale" target="_blank" class=""><img src="https://docs.google.com/uc?export=download&id=1f0O6L3S8BvNrR0hvQJwoW7n0T4IXaS3u&revid=0B_4a-5REfZ5jMzdMRWdac1VDTUMvT256WEFuQzY4NjZkYnFvPQ" alt="LinkedIn" class=""></a></span><span style="font-size:12.8px;font-family:arial,sans-serif;color:rgb(34,34,34)" class="">   <a href="https://github.com/stIncMale" target="_blank" class=""><img src="https://docs.google.com/uc?export=download&id=1nyhfak_LwBgvTHHlSsAMo-9f2j_Fj7SA&revid=0B_4a-5REfZ5jLzdNdTB1V05GL3BIWG9CM3ZsbnNwc1hRTGdJPQ" alt="GitHub" class=""></a></span><span style="font-size:12.8px;font-family:arial,sans-serif;color:rgb(34,34,34)" class="">   <a href="https://www.youtube.com/user/stIncMale" target="_blank" class=""><img src="https://docs.google.com/uc?export=download&id=1qdEbsKopsuQlOwTkcvlJwxfxhNhBArrc&revid=0B_4a-5REfZ5jd0JiM0h5RExCYkx5WEpZQyt5OGtaRlRkVU9zPQ" alt="YouTube" class=""></a></span></div><div class=""><div style="color:rgb(102,102,102);font-family:"Open Sans",sans-serif;font-size:16px" class=""></div></div></div></div></div></div></div></div></div></div></div></div></div></div><br class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Sun, 12 Aug 2018 at 12:34, <<a href="mailto:concurrency-interest-request@cs.oswego.edu" class="">concurrency-interest-request@cs.oswego.edu</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Send Concurrency-interest mailing list submissions to<br class="">
        <a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a><br class="">
<br class="">
To subscribe or unsubscribe via the World Wide Web, visit<br class="">
        <a href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest" rel="noreferrer" target="_blank" class="">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a><br class="">
or, via email, send a message with subject or body 'help' to<br class="">
        <a href="mailto:concurrency-interest-request@cs.oswego.edu" target="_blank" class="">concurrency-interest-request@cs.oswego.edu</a><br class="">
<br class="">
You can reach the person managing the list at<br class="">
        <a href="mailto:concurrency-interest-owner@cs.oswego.edu" target="_blank" class="">concurrency-interest-owner@cs.oswego.edu</a><br class="">
<br class="">
When replying, please edit your Subject line so it is more specific<br class="">
than "Re: Contents of Concurrency-interest digest..."<br class="">
<br class="">
<br class="">
Today's Topics:<br class="">
<br class="">
   1. Re: Visibility of array initialization (Justin Sampson)<br class="">
   2. Re: Visibility of array initialization (Yuval Shavit)<br class="">
<br class="">
<br class="">
----------------------------------------------------------------------<br class="">
<br class="">
Message: 1<br class="">
Date: Sun, 12 Aug 2018 17:44:11 +0000<br class="">
From: Justin Sampson <<a href="mailto:jsampson@guidewire.com" target="_blank" class="">jsampson@guidewire.com</a>><br class="">
To: Raph Frank <<a href="mailto:raphfrk@gmail.com" target="_blank" class="">raphfrk@gmail.com</a>>,<br class="">
        "<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>"<br class="">
        <<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>><br class="">
Subject: Re: [concurrency-interest] Visibility of array initialization<br class="">
Message-ID: <<a href="mailto:0801B8EC-BDA5-4AFF-97F6-EE483A0AD2AF@guidewire.com" target="_blank" class="">0801B8EC-BDA5-4AFF-97F6-EE483A0AD2AF@guidewire.com</a>><br class="">
Content-Type: text/plain; charset="utf-8"<br class="">
<br class="">
The spec explicitly calls out the length of an array as being a final field, so it's always guaranteed to be seen correctly:<br class="">
<br class="">
<a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.7" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.7</a><br class="">
<a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.5" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.5</a><br class="">
<br class="">
But array elements are just regular heap variables, initialized to 0 before the initializer expression is evaluated:<br class="">
<br class="">
<a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.6" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.6</a><br class="">
<a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1</a><br class="">
<br class="">
So yeah, I'm pretty sure that means you could see 0's if the array isn't published safely.<br class="">
<br class="">
Cheers,<br class="">
Justin<br class="">
<br class="">
<br class="">
From: Concurrency-interest <<a href="mailto:concurrency-interest-bounces@cs.oswego.edu" target="_blank" class="">concurrency-interest-bounces@cs.oswego.edu</a>> on behalf of "<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>" <<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>><br class="">
Reply-To: Raph Frank <<a href="mailto:raphfrk@gmail.com" target="_blank" class="">raphfrk@gmail.com</a>><br class="">
Date: Sunday, August 12, 2018 at 7:29 AM<br class="">
To: "<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>" <<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>><br class="">
Subject: [concurrency-interest] Visibility of array initialization<br class="">
<br class="">
I was wondering what are the visibility rules for array initialization.<br class="">
<br class="">
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)?<br class="">
<br class="">
Thread 1<br class="">
int[] array = new int[] {-1, -1, -1};<br class="">
thread2.start();<br class="">
<br class="">
......<br class="">
<br class="">
array = new int[] {1, 2, 3};<br class="">
<br class="">
Thread 2<br class="">
System.out.println(array[2]);<br class="">
<br class="">
Is it possible for the 2nd thread to print zero?  Is it guaranteed to print -1 or 2?<br class="">
<br class="">
The spec refers to final fields in the constructor of objects and also says that arrays referenced by final fields are visible (including elements).<br class="">
<br class="">
" 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. "<br class="">
<br class="">
It doesn't give any indication of what happens when initializing raw arrays.  Does the array initialization count as a constructor?<br class="">
<br class="">
Is the logical process that arrays are set to all zeros at time zero and then they are initialized when created?<br class="">
-------------- next part --------------<br class="">
An HTML attachment was scrubbed...<br class="">
URL: <<a href="http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180812/8eaa907a/attachment-0001.html" rel="noreferrer" target="_blank" class="">http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180812/8eaa907a/attachment-0001.html</a>><br class="">
<br class="">
------------------------------<br class="">
<br class="">
Message: 2<br class="">
Date: Sun, 12 Aug 2018 14:21:01 -0400<br class="">
From: Yuval Shavit <<a href="mailto:yankee.sierra@gmail.com" target="_blank" class="">yankee.sierra@gmail.com</a>><br class="">
To: <a href="mailto:jsampson@guidewire.com" target="_blank" class="">jsampson@guidewire.com</a><br class="">
Cc: <a href="mailto:raphfrk@gmail.com" target="_blank" class="">raphfrk@gmail.com</a>,  "<a href="mailto:Concurrency-interest@cs.oswego.edu" target="_blank" class="">Concurrency-interest@cs.oswego.edu</a>"<br class="">
        <<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>><br class="">
Subject: Re: [concurrency-interest] Visibility of array initialization<br class="">
Message-ID:<br class="">
        <CAE+h5-B6VpOeXiz-bgNGn=p32r-uiDhzZL5y1p+y21jE1f=<a href="mailto:eaw@mail.gmail.com" target="_blank" class="">eaw@mail.gmail.com</a>><br class="">
Content-Type: text/plain; charset="utf-8"<br class="">
<br class="">
I believe the value will be -1 or 2, but not 0 -- not for any special array<br class="">
semantics, but because the call to Thread::start introduces a<br class="">
happens-before (JLS 17.4.5).<br class="">
<br class="">
On Sun, Aug 12, 2018 at 1:48 PM Justin Sampson via Concurrency-interest <<br class="">
<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>> wrote:<br class="">
<br class="">
> The spec explicitly calls out the *length* of an array as being a final<br class="">
> field, so it's always guaranteed to be seen correctly:<br class="">
><br class="">
><br class="">
><br class="">
> <a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.7" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.7</a><br class="">
><br class="">
> <a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.5" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.5</a><br class="">
><br class="">
><br class="">
><br class="">
> But array elements are just regular heap variables, initialized to 0<br class="">
> before the initializer expression is evaluated:<br class="">
><br class="">
><br class="">
><br class="">
> <a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.6" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.6</a><br class="">
><br class="">
> <a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1" rel="noreferrer" target="_blank" class="">https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1</a><br class="">
><br class="">
><br class="">
><br class="">
> So yeah, I'm pretty sure that means you could see 0's if the array isn't<br class="">
> published safely.<br class="">
><br class="">
><br class="">
><br class="">
> Cheers,<br class="">
><br class="">
> Justin<br class="">
><br class="">
><br class="">
><br class="">
><br class="">
><br class="">
> *From: *Concurrency-interest <<a href="mailto:concurrency-interest-bounces@cs.oswego.edu" target="_blank" class="">concurrency-interest-bounces@cs.oswego.edu</a>><br class="">
> on behalf of "<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>" <<br class="">
> <a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>><br class="">
> *Reply-To: *Raph Frank <<a href="mailto:raphfrk@gmail.com" target="_blank" class="">raphfrk@gmail.com</a>><br class="">
> *Date: *Sunday, August 12, 2018 at 7:29 AM<br class="">
> *To: *"<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>" <<br class="">
> <a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank" class="">concurrency-interest@cs.oswego.edu</a>><br class="">
> *Subject: *[concurrency-interest] Visibility of array initialization<br class="">
><br class="">
><br class="">
><br class="">
> I was wondering what are the visibility rules for array initialization.<br class="">
><br class="">
><br class="">
><br class="">
> If an array is initialized and then another thread gets access to a<br class="">
> reference to that array, are the elements guaranteed to be at least their<br class="">
> initial state (or could the array be all zeros)?<br class="">
><br class="">
><br class="">
><br class="">
> *Thread 1*<br class="">
><br class="">
> int[] array = new int[] {-1, -1, -1};<br class="">
><br class="">
> thread2.start();<br class="">
><br class="">
><br class="">
><br class="">
> ......<br class="">
><br class="">
><br class="">
><br class="">
> array = new int[] {1, 2, 3};<br class="">
><br class="">
><br class="">
><br class="">
> *Thread 2*<br class="">
><br class="">
> System.out.println(array[2]);<br class="">
><br class="">
><br class="">
><br class="">
> Is it possible for the 2nd thread to print zero?  Is it guaranteed to<br class="">
> print -1 or 2?<br class="">
><br class="">
><br class="">
><br class="">
> The spec refers to final fields in the constructor of objects and also<br class="">
> says that arrays referenced by final fields are visible (including<br class="">
> elements).<br class="">
><br class="">
><br class="">
><br class="">
> " It will also see versions of any object or array referenced by those<br class="">
> final fields that are at least as up-to-date as the final fields are. "<br class="">
><br class="">
><br class="">
><br class="">
> It doesn't give any indication of what happens when initializing raw<br class="">
> arrays.  Does the array initialization count as a constructor?<br class="">
><br class="">
><br class="">
><br class="">
> Is the logical process that arrays are set to all zeros at time zero and<br class="">
> then they are initialized when created?<br class="">
> _______________________________________________<br class="">
> Concurrency-interest mailing list<br class="">
> <a href="mailto:Concurrency-interest@cs.oswego.edu" target="_blank" class="">Concurrency-interest@cs.oswego.edu</a><br class="">
> <a href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest" rel="noreferrer" target="_blank" class="">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a><br class="">
><br class="">
-------------- next part --------------<br class="">
An HTML attachment was scrubbed...<br class="">
URL: <<a href="http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180812/89a0c6e3/attachment.html" rel="noreferrer" target="_blank" class="">http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20180812/89a0c6e3/attachment.html</a>><br class="">
<br class="">
------------------------------<br class="">
<br class="">
Subject: Digest Footer<br class="">
<br class="">
_______________________________________________<br class="">
Concurrency-interest mailing list<br class="">
<a href="mailto:Concurrency-interest@cs.oswego.edu" target="_blank" class="">Concurrency-interest@cs.oswego.edu</a><br class="">
<a href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest" rel="noreferrer" target="_blank" class="">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a><br class="">
<br class="">
<br class="">
------------------------------<br class="">
<br class="">
End of Concurrency-interest Digest, Vol 162, Issue 8<br class="">
****************************************************<br class="">
</blockquote></div></div>
_______________________________________________<br class="">Concurrency-interest mailing list<br class=""><a href="mailto:Concurrency-interest@cs.oswego.edu" class="">Concurrency-interest@cs.oswego.edu</a><br class="">http://cs.oswego.edu/mailman/listinfo/concurrency-interest<br class=""></div></blockquote></div><br class=""></div></div></body></html>