<div dir="ltr">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).</div><br><div class="gmail_quote"><div dir="ltr">On Sun, Aug 12, 2018 at 1:48 PM Justin Sampson via Concurrency-interest <<a href="mailto:concurrency-interest@cs.oswego.edu">concurrency-interest@cs.oswego.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">







<div bgcolor="white" lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="m_-3550585470714406787WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">The spec explicitly calls out the
<i>length</i> of an array as being a final field, so it's always guaranteed to be seen correctly:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.7" target="_blank">https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.7</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.5" target="_blank">https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.5</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">But array elements are just regular heap variables, initialized to 0 before the initializer expression is evaluated:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.6" target="_blank">https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.6</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><a href="https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1" target="_blank">https://docs.oracle.com/javase/specs/jls/se10/html/jls-17.html#jls-17.4.1</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">So yeah, I'm pretty sure that means you could see 0's if the array isn't published safely.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Cheers,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Justin<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From: </span>
</b><span style="font-family:Calibri;color:black">Concurrency-interest <<a href="mailto:concurrency-interest-bounces@cs.oswego.edu" target="_blank">concurrency-interest-bounces@cs.oswego.edu</a>> on behalf of "<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank">concurrency-interest@cs.oswego.edu</a>" <<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank">concurrency-interest@cs.oswego.edu</a>><br>
<b>Reply-To: </b>Raph Frank <<a href="mailto:raphfrk@gmail.com" target="_blank">raphfrk@gmail.com</a>><br>
<b>Date: </b>Sunday, August 12, 2018 at 7:29 AM<br>
<b>To: </b>"<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank">concurrency-interest@cs.oswego.edu</a>" <<a href="mailto:concurrency-interest@cs.oswego.edu" target="_blank">concurrency-interest@cs.oswego.edu</a>><br>
<b>Subject: </b>[concurrency-interest] Visibility of array initialization<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<div>
<p class="MsoNormal">I was wondering what are the visibility rules for array initialization.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">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)?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><b>Thread 1</b><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">int[] array = new int[] {-1, -1, -1};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">thread2.start();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">......<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">array = new int[] {1, 2, 3};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal"><b>Thread 2</b><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">System.out.println(array[2]);<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Is it possible for the 2nd thread to print zero?  Is it guaranteed to print -1 or 2?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">The spec refers to final fields in the constructor of objects and also says that arrays referenced by final fields are visible (including elements).<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">" It will also see versions of any object or array referenced by those
<code><span style="font-size:10.0pt">final</span></code> fields that are at least as up-to-date as the
<code><span style="font-size:10.0pt">final</span></code> fields are. "<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">It doesn't give any indication of what happens when initializing raw arrays.  Does the array initialization count as a constructor?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Is the logical process that arrays are set to all zeros at time zero and then they are initialized when created?<u></u><u></u></p>
</div>
</div>
</div>
</div>

_______________________________________________<br>
Concurrency-interest mailing list<br>
<a href="mailto:Concurrency-interest@cs.oswego.edu" target="_blank">Concurrency-interest@cs.oswego.edu</a><br>
<a href="http://cs.oswego.edu/mailman/listinfo/concurrency-interest" rel="noreferrer" target="_blank">http://cs.oswego.edu/mailman/listinfo/concurrency-interest</a><br>
</blockquote></div>