# Why is this execution happens-before consistent?

In section 17.4.5 "Happens-before Order" of Java Language Specification, a trace is examined for the notion of happens-before consistency.

The trace is shown in the figure in which $A$ and $B$ are shared variables with initially $A = B = 0$.

An execution order of the trace is as follows.

In this execution, the reads see writes that occur later in the execution order. The document says that it is happens-before consistent. I am quite confused about this. In my opinion, this execution order does not even satisfy program order. Why is it happens-before consistent?

• The trace is presented in inverse order, i.e., A = 2 is executed first, and r2 = A is executed last. – Yuval Filmus Mar 3 '14 at 18:50
• I don't understand it. The document remarks that In this execution, the reads see writes that occur later in the execution order.  How could this happen? – hengxin Mar 4 '14 at 1:39
• I suggest you read the entire subsection. It seems that the idea is that although the writes occur out of order, semantically they occur in order. For this trickery to work, the reads should behave as if the previous (wrt program order) writes have happened, even though in reality they "happen" later. – Yuval Filmus Mar 4 '14 at 2:15