While reading Concurrency in practice I read that:
NoVisibilitydemonstrated one of the ways that insufficiently synchronized programs can cause surprising results: stale data. When the reader thread examinesready, it may see an out of date value. Unless synchronization is used every time a variable is accessed, it is possible to see a stale value for that variable. Worse, staleness is not all or nothing: a thread can see an up-to-date value of one variable but a stale value of another variable that was written first.
public class NoVisibility {
private static boolean ready;
private static int number;
private static class ReaderThread extends Thread {
public void run() {
while (!ready)
Thread.yield();
System.out.println(number);
}
}
public static void main(String[] args) {
new ReaderThread().start();
number = 42;
ready = true;
}
}
I am not getting meaning of stale. Both threads are sharing the same reference, how can a value modified by one be or not be visible to other thread?