可见性

阅读更多
public class NoVisibility {

    private static boolean ready;

    private static int     number;

    private static class ReaderThread extends Thread {

        @Override
        public void run() {
            while (!ready) {
                Thread.yield();
            }
            System.out.println(number);
        }

    }

    public static void main(String args[]) {
        new ReaderThread().start();
        number = 42;
        ready = true;
    }
}


从线程共享对象的可见性来分析下这个程序有什么问题!

你可能感兴趣的:(thread,Java)