title: Can a Java Compiler or JVM swap instruction order of independent instructions?
body: <p>Let's take following statements:</p>
  
  <pre><code>int d0, d1;
  int[] ds = {0, 0};
  </code></pre>
  
  <p>Now one thread has following instructions:</p>
  
  <pre><code>d0++;
  d1++;
  </code></pre>
  
  <p>while the other thread has this instruction:</p>
  
  <pre><code>ds[1] = d1;
  ds[0] = d0;
  </code></pre>
  
  <p>If we run these threads in parallel, there are obviously three combinations that <code>ds</code> can look like: {0, 0}, {1, 1}, and {1, 0}.</p>
  
  <p>Now the big question is: Can there also be {0, 1}? Can the Compiler/JVM simply swap instruction because it thinks they are unrelated? If yes, what exactly are the "rules" for such behaviour and is it up to the compiler or the JVM?</p>