If Thread B wishes to see changes Thread A makes, can only the last change be to a volatile variable as opposed to all?

前端 未结 1 791
深忆病人
深忆病人 2021-01-01 04:02

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

1条回答
  •  隐瞒了意图╮
    2021-01-01 04:45

    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.

    0 讨论(0)
提交回复
热议问题