---
title: "可见性"
author: "Goodtiger"
date: "2008-09-15T15:27:54Z"
tags: ["Java", "thread"]
slug: "untitled"
draft: false
lastmod: "2008-09-15T15:27:54Z"
id: 242484
url: "https://goodtiger.iteye.com/blog/242484"
---

```java
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;
    }
}
```


从线程共享对象的可见性来分析下这个程序有什么问题！