Given the below code:
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 ();
}
}
Is it guaranteed (under the Java Memory Model) that at least one thread prints true
?
I understand that there is a happens-before relationship between a write to a volatile variable and a read that sees the updated value, but it seems me it is possible for none of the threads to see the updated value, although I couldn't make it happen.