title: Java compiler reordering
body: <p>Today I read java interview questions and I read this question:
  Question : Consider the following Java code snippet, which is initializing two variables and both are not volatile, and two threads T1 and T2 are modifying these values as following, both are not synchronized</p>
  
  <pre><code>int x = 0;
  boolean bExit = false;
  
  Thread 1 (not synchronized)
  x = 1; 
  bExit = true;
  
  Thread 2 (not synchronized)
  if (bExit == true) 
  System.out.println("x=" + x);
  </code></pre>
  
  <p>Now tell us, is it possible for Thread 2 to print “x=0”?</p>
  
  <p>So, the answer is "yes". In the explanation there is "because without any instruction to compiler e.g. synchronized or volatile, bExit=true might come before x=1 in compiler reordering." Before that I don't know that the compiler can execute one line before another line after it. </p>
  
  <p>Why is this reordering ? And what if I print something to the console from different thread - the line that is supposed to be print first will be print after the line that is supposed to be print second (if they are printed from the same thread) ? It's weird to me (maybe, because I saw this thing for reordering for the first time). Can someone give some explanation ? </p>