Consider two threads:
A==B==0 (initially)
| Thread 1 | Thread 2 |
|---|---|
| B=42; | if (A==1) |
| A=1; | ...print(B) |
To my knowledge if (at least) A is volatile we will only be able to read B==42 at the print. Though if only mark B as volatile we can read B==42 but also B==0.
I want to look at the case where only B is volatile more closely and understand why we can read B==0 based on what these docs say. To do so I started by adding all program order edges and synchronizes with as described in the docs:
The two edges from B=42 to A=1 are simple program order (PO) edges the rest are synchronizes with (SW) edges. According to the docs we have a SW edge when "The write of the default value [...] to each variable synchronizes-with the first action in every thread." (those are the first 4 edges in the picture) and "A write to a volatile variable v [...] synchronizes-with all subsequent reads of v" (the edges from B=42 to print(B)).
Now we can take a look at what happens before edges exists (HB), according to the docs each of these edges is also an HB ordering. Additionally for all hb(x,y) and hb(y,z) we have hb(x,z) (these edges are missing but we will still use those).
Finally, we get from the docs what we can read at print(B) from: "We say that a read r of a variable v is allowed to observe a write w to v if, in the happens-before partial order [...]:
- r is not ordered before w (i.e., it is not the case that hb(r, w)), and
- there is no [...] no write w' to v such that hb(w, w') and hb(w', r) "
Let's see if we can observe a write w (B=0) at a read r (print(B)). We indeed have not hb(r, w). However, we do have a write w' (B=42) intervening with hb(wow') and hb(w',r).
This makes me wonder can we observe B==0 at the print and if yes where is my reasoning or understanding of the docs wrong? I would like a answer that is clearly referring to the docs.
(I have looked at this post however I hope for an explanation referencing the JMM docs more closely, my question also arises from this particular code)

Just to make sure I understand you correctly:
We are interested in the execution where:
AinA == 1reads1BinSystem.out.print(B)reads0In terms of the JMM actions the execution is this (for brevity only actions on
AandBare shown):Here are program-order and synchronizes-with (
[po]and[sw]on the diagram) relations between the actions in the execution:Notes:
there is an
[sw]edge between initial writes and the first action in every thread because of this rule:volatile-read(B):0synchronizes-withvolatile-write(B=0)because of this rule:BTW the synchronization order in our case is:
volatile-write(B=0)->volatile-read(B):0->volatile-write(B=42).According to the synchronization order's definition it's a global order among all synchronization actions and it's consistent with the program order.
The read
volatile-read(B):0returns0because of this rule:there is no
[sw]edge betweenvolatile-write(B=42)andvolatile-read(B):0. That's because according to the following rule a volatile write synchronizes-with only those volatile reads of the variable, that come later in the synchronization order:there is no
[sw]edge betweenwrite(A=1)andread(A):1because these are ordinary write and read, but[sw]is only for synchronization actionsAnd here are happens-before relations (built from
[po]and[sw]) between the actions:Happens-before consistency according to the JLS:
Happens-before consistency isn't violated for
read(A):1because, as you can see in the diagram above, there is no happens-before relations betweenwrite(A=1)andread(A):1.volatile-read(B):0is also fine (it's explained above).In fact, I don't see anything in the JMM that is violated in this execution - so IMO the execution is legal according to the JMM.