I\'ve looked at this answer, and it states how:
Under the new memory model, when thread A writes to a volatile variable V, and thread B reads from V
Yes, the change to value
is guaranteed to be visible to thread b.
JLS 17.4.4. Synchronization Order says:
- A write to a volatile variable v (§8.3.1.4) synchronizes-with all subsequent reads of v by any thread (where "subsequent" is defined according to the synchronization order).
JLS 17.4.5. Happens-before Order says:
Two actions can be ordered by a happens-before relationship. If one action happens-before another, then the first is visible to and ordered before the second.
If we have two actions x and y, we write hb(x, y) to indicate that x happens-before y.
If x and y are actions of the same thread and x comes before y in program order, then hb(x, y).
There is a happens-before edge from the end of a constructor of an object to the start of a finalizer (§12.6) for that object.
If an action x synchronizes-with a following action y, then we also have hb(x, y).
If hb(x, y) and hb(y, z), then hb(x, z).
Bullet 1 says that value = 1
happens-before read = true
.
Bullet 3 says that read = true
happens-before !read
.
Bullet 1 says that !read
happens-before "Value: " + value
.
Bullet 4 says that value = 1
happens-before "Value: " + value
.