I'm reading Paul Butcher's "Seven concurrency models in seven weeks", it gives sample code "puzzle.java" in chapter 2.2:
ThreadsLocks/Puzzle/src/main/java/com/paulbutcher/Puzzle.java
public class Puzzle {
static boolean answerReady = false;
static int answer = 0;
static Thread t1 = new Thread() {
public void run() {
answer = 42;
answerReady = true;
}
};
static Thread t2 = new Thread() {
public void run() {
if (answerReady) System.out.println("The meaning of life is: " + answer);
else System.out.println("I don't know the answer");
}
};
public static void main(String[] args) throws InterruptedException {
t1.start(); t2.start(); t1.join(); t2.join();
} }
So here is a racing condition.
Then it says,
Imagine that we rewrote run() as follows:
public void run() {
while (!answerReady)
Thread.sleep(100);
System.out.println("The meaning of life is: " + answer);
}
Our program may never exit because answerReady may never appear to become true.
May I ask why?