Visibility of mutable instance objects

Viewed 73

As per the book “Java concurrency in practice”, the below code might execute forever or value of number may still be 0 when ready is true and the recommendation was to define the variables as volatile. However the below listed program always seems to return the correct value (42) instead of stale value even after I tried this multiple times. Is this a phenomenon that occurs very rarely?

public class NoVisibility {
    private static boolean ready;
    private static int number;

    private static class ReaderThread extends Thread{
        public void run(){
            System.out.println("Thread started =" + ready + " " + number);
            while(!ready){
                Thread.yield();
            }
            System.out.println("value is" + number);
        }
    }

    public static void main(String[] args) throws InterruptedException {
        new ReaderThread().start();
        Thread.sleep(10000);
        number = 42;
        ready = true;
    }
}
2 Answers

Is this a phenomenon that occurs very rarely

Yes, rare. However, given computers that do things billions of times per second, rare is quite common.

Whenever accessing resources across threads, you must address thread-safety. Otherwise, your app in deployment will suffer sporadic bugs that are terribly difficult if not impossible to solve.

By this same logic, testing for concurrency bugs is notoriously difficult, as you have seen in your Question.

Always look to avoid threaded code where possible. For example, utilize immutable objects or defensive copies.

Required reading: Java Concurrency in Practice by Brian Goetz, et al.

Author says server JVM performs more optimisation then the client JVM. a paragraph fromt he book

Related