多个volatile变量之间的同步

Synchronization between multiple volatile variables

给定以下代码:

public class Test {

  private volatile boolean a;

  private volatile boolean b;

  private void one () {
    a = true;
    System.out.println (b);
  }

  private void two () {
    b = true;
    System.out.println (a);
  }

  public static void main (String[] args) throws Exception {
    Test s = new Test ();
    Thread one = new Thread (s::one);
    Thread two = new Thread (s::two);
    one.start ();
    two.start ();
    one.join ();
    two.join ();
  }

}

是否保证(在Java内存模型下)至少一个线程打印true

我知道写入 volatile 变量和读取更新值之间存在先行关系,但我认为 none 线程有可能看到更新值,虽然我没能做到。

是的,这是有保证的。

为了证明这一点,不失一般性地假设线程 1 打印 false。由于 b 是易变的,这意味着线程 1 在线程 2 写入 b 之前执行打印。但如果是这种情况,那么在线程 2 执行自己的打印时,a 一定已经被线程 1 设置为 true

请注意,根据 JLS §17.4.5:

,在写入之前不能重新排序打印件
  • If x and y are actions of the same thread and x comes before y in program order, then hb(x, y) [x happens-before y].

此外,对 ab 的写入将立即对其他线程可见:

  • A write to a volatile field (§8.3.1.4) happens-before every subsequent read of that field.