多线程中,main线程结束之后,从线程怎么还在执行啊,请看以下代码:
class JoinThread extends Thread
{
private int i;
public void run()
{
try
{
sleep(10);
for (; i < 1000 ; i++ )
{
System.out.println(getName() + " " + i);
}
}
catch (Exception e)
{
System.out.println(e.toString());
}
}
public static void main(String[] args)
{
JoinThread jt = new JoinThread();
jt.start();
System.out.println("Hello World!");
}
}
执行结果如下:
Hello World!
Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
Thread-0 6
Thread-0 7
Thread-0 8
Thread-0 9
Thread-0 10
。。。。
为什么主线程都打印hello world结束了,Thread0还在执行,而且thread0还休眠了10s让出了处理器 |