title: Instruction reordering & happens-before relationship
body: <p>In the book Java Concurrency In Practice, we are told several time that the instructions of our program can be reordered, either by the compiler, by the JVM at runtime, or even by the processor. So we should assume that the executed program will not have its instructions executed in exactly the same order than what we specified in the source code.</p>
  
  <p>However, the last chapter discussing Java Memory Model provides a listing of <strong>happens-before</strong> rules indicating which instruction ordering are preserved by the JVM. The first of these rules is:</p>
  
  <ul>
  <li>"Program order rule. Each action in a thread happens before every action in that thread that comes later in the program order."</li>
  </ul>
  
  <p>I believe "program order" refers to the source code.</p>
  
  <p><strong>My question</strong>: assuming this rule, I wonder what instruction may be actually reordered.</p>
  
  <p>"Action" is defined as follow: </p>
  
  <blockquote>
    <p>The Java Memory Model is specified in terms of actions, which include reads and writes to variables, locks and unlocks of monitors, and starting and joining with threads. The JMM defines a partial ordering called happens before on all actions within the program. To guarantee that the thread executing action B can see the results of action A (whether or not A and B occur in different threads), there must be a happens before relationship between A and B. In the absence of a happens before ordering between two operations, the JVM is free to reorder them as it pleases.</p>
  </blockquote>
  
  <p>Other order rules mentionned are:</p>
  
  <ul>
  <li>Monitor lock rule. An unlock on a monitor lock happens before every subsequent lock on that same monitor lock.</li>
  <li>Volatile variable rule. A write to a volatile field happens before every subsequent read of that same field.</li>
  <li>Thread start rule. A call to Thread.start on a thread happens before every action in the started thread.</li>
  <li>Thread termination rule. Any action in a thread happens before any other thread detects that thread has terminated, either by successfully return from Thread.join or by Thread.isAlive returning false.</li>
  <li>Interruption rule. A thread calling interrupt on another thread happens before the interrupted thread detects the interrupt (either by having InterruptedException thrown, or invoking isInterrupted or interrupted).</li>
  <li>Finalizer rule. The end of a constructor for an object happens before the start of the finalizer for that object.</li>
  <li>Transitivity. If A happens before B, and B happens before C, then A happens before C.</li>
  </ul>