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.

你可能感兴趣的:(java 多线程内存可见性问题(volatile))