Java Concurrency in Practice explains this concept:
When a thread reads a variable without synchronization, it may see a stale value, but at least it sees a value that was actually placed there by some thread rather than some random value. This safety guarantee is called out-of-thin-air safety.
Is this type of safety weak since it may include a stale value?
Perhaps this snippet, at least it sees a value that was actually placed there by some thread than some random value
, was mentioned since the book's previous topic was the possibility of the JVM to re-order variable statements in reference to sharing variables without synchronization
?
Example: Depending on re-ordering: 42 or 0 could print out.
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;
}
}
EDITED - removed "please comment" remark.
See Question&Answers more detail:os