在我看来,这个程序的输出只能是
Hello 0 1 2 3 4 Yes
但答案列出
0 1 2 3 4 Hello Yes
作为一个可能的答案。我的问题是当测试进入睡眠状态时,不应该 main 是唯一进入运行状态的其他线程,这样 Hello 应该总是首先打印吗?
public class Lean
{
public static void main(String args[]) throws Exception
{
Test test = new Test();
test.start();
System.out.print("Hello ");
test.join();
System.out.print("Yes");
}
}
class Test extends Thread
{
public void run()
{
try
{
Thread.sleep(2000);
} catch (InterruptedException e)
{}
for (int counter=0; counter<5 ; counter++)
{
System.out.print(counter + " ");
}
}
}