In Java Concurrency in practice, there is a pattern that confused me:
public class Novisibility { private static boolean ready; private static int number; private static class ReaderThread implements Runnable { public void run() { while (!ready) { Thread.yield(); } System.out.println(number); } } public static void main(String[] args) { System.out.println("0"); new Thread(new ReaderThread()).run(); System.out.println("1"); number = 42; System.out.println("2"); ready = true; System.out.println("3"); } }
I understand that reordering causes the loop to never break, but I cannot understand why "1", "2", and "3" never print to the console. Can any organ help?
source share