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

安稳与你 提交于 2019-11-30 10:13:14

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!