0

The code snippet in 《Java Concurrency in Practice》 :

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;
    }
}

In this book, the author said "NoVisibility class may print 0 or 42", but I ran this code several times, the result is always 42.

Jimmy Zhang
  • 939
  • 1
  • 10
  • 15
  • What's the question? Also, does this pertain sharing a field between processes or between two threads? – entpnerd Jan 15 '16 at 04:27
  • the code block in the duplicate is exactly the same and the question is worded differently but the answer is much more detailed and involved. –  Jan 15 '16 at 04:27

0 Answers0