package netease;
public class VolatileTest {
private static int number;
private static boolean ready;
private static class ReadThread extends Thread{
public void run(){
while(!ready)
Thread.yield();
System.out.println(number);
}
}
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
new ReadThread().start();
number = 42;
ready = true;
}
}
1) 上面的代码输出什么,并说明原因?
2)若ready是volatile修饰,输出什么,说明原因? |
|