同志们,下面这段代码在你们机器上输出什么?请亲自测试后再回复!!
public class Test{
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);
}
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
new ReaderThread().start();
Thread.sleep(1000);
number = 42;
ready = true;
}}
public class Test{
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);
}
}
/**
* @param args
* @throws InterruptedException
*/
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
new ReaderThread().start();
Thread.sleep(1000);
number = 42;
ready = true;
}}
正好开着eclipse,没看程序直接运行的结果