Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Am I correct in my understanding that due to out-of-order execution I can get a NullPointerException? In other words: there's no guarantee that because I read a non-null "b" I'll read a non-null "a"?</p> </blockquote> <p>Assuming that the values assigned to <code>a</code> and <code>b</code> or non-null, I think your understanding is <strong>not correct</strong>. The JLS says this:</p> <blockquote> <p>(<strong>1</strong>) If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).</p> <p>(<strong>2</strong>) If an action x synchronizes-with a following action y, then we also have hb(x, y).</p> <p>(<strong>3</strong>) If hb(x, y) and hb(y, z), then hb(x, z). </p> </blockquote> <p>and</p> <blockquote> <p>(<strong>4</strong>) A write to a volatile variable (§8.3.1.4) <code>v</code> synchronizes-with all subsequent reads of <code>v</code> <em>by any thread</em> (where subsequent is defined according to the synchronization order).</p> </blockquote> <p><strong>Theorem</strong></p> <blockquote> <p>Given that thread #1 has called <code>setBoth(...);</code> once, and that the arguments were non-null, and that thread #2 has observed <code>b</code> to be non-null, then thread #2 cannot then observe <code>a</code> to be null.</p> </blockquote> <p><strong>Informal Proof</strong></p> <ol> <li>By (<strong>1</strong>) - hb(write(a, non-null), write(b, non-null)) in thread #1 </li> <li>By (<strong>2</strong>) and (<strong>4</strong>) - hb(write(b, non-null), read(b, non-null))</li> <li>By (<strong>1</strong>) - hb(read(b, non-null), read(a, XXX)) in thread #2,</li> <li>By (<strong>4</strong>) - hb(write(a, non-null), read(b, non-null))</li> <li>By (<strong>4</strong>) - hb(write(a, non-null), read(a, XXX))</li> </ol> <p>In other words, the write of a non-null value to <code>a</code> "happens-before" the read of the value (XXX) of <code>a</code>. The only way that XXX can be null, is if there was some other action writing null to <code>a</code> such that hb(write(a,non-null), write(a,XXX)) and hb(write(a,XXX), read(a,XXX)). And this is impossible according to the problem definition, and therefore XXX cannot be null. QED.</p> <p><strong>Explanation</strong> - the JLS states that the hb(...) ("happens-before") relationship does not totally forbid reordering. However, if hb(xx,yy), then reordering of actions xx and yy is <em>only</em> allowed if the resulting code has the same observable effect as the original sequence. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload