java threads don't see shared boolean changes
- by andymur
Here the code
class Aux implements Runnable {
private Boolean isOn = false;
private String statusMessage;
private final Object lock;
public Aux(String message, Object lock) {
this.lock = lock;
this.statusMessage = message;
}
@Override
public void run() {
for (;;) {
synchronized (lock) {
if (isOn && "left".equals(this.statusMessage)) {
isOn = false;
System.out.println(statusMessage);
} else if (!isOn && "right".equals(this.statusMessage)) {
isOn = true;
System.out.println(statusMessage);
}
if ("left".equals(this.statusMessage)) {
System.out.println("left " + isOn);
}
}
}
}
}
public class Question {
public static void main(String [] args) {
Object lock = new Object();
new Thread(new Aux("left", lock)).start();
new Thread(new Aux("right", lock)).start();
}
}
In this code I expect to see:
left, right, left right and so on,
but when Thread with "left" message changes isOn to false, Thread with "right" message don't see it and I get ("right true" and "left false" console messages), left thread don't get isOn in true, but right Thread can't change it cause it always see old isOn value (true).
When i add volatile modifier to isOn nothing changes, but if I change isOn to some class with boolean field and change this field then threads are see changes and it works fine
Thanks in advance.