java 多线程内存可见性问题(volatile)

  • 摘抄自(http://tutorials.jenkov.com/java-concurrency/volatile.html

  • 机翻
    完全不稳定的可见性保证
    实际上,Java volatile 的可见性保证超出了 volatile 变量本身。 可见性保证如下:

  • 如果线程 A 写入一个 volatile 变量,而线程 B 随后读取了同一个 volatile 变量,那么线程 A 在写入 volatile 变量之前对线程 A 可见的所有变量,在线程 B 读取 volatile 变量之后也将对线程 B 可见。

  • 如果线程 A 读取一个 volatile 变量,那么在读取 volatile 变量时线程 A 可见的所有变量也将从主存储器中重新读取。

  • 原文
    Full volatile Visibility Guarantee
    Actually, the visibility guarantee of Java volatile goes beyond the volatile variable itself. The visibility guarantee is as follows:

  • If Thread A writes to a volatile variable and Thread B subsequently reads the same volatile variable, then all variables visible to Thread A before writing the volatile variable, will also be visible to Thread B after it has read the volatile variable.

  • If Thread A reads a volatile variable, then all all variables visible to Thread A when reading the volatile variable will also be re-read from main memory.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容