Sar*_*nan 27 java multithreading jvm
我读了这句话:
主线程必须是完成执行的最后一个线程.当主线程停止时,程序终止.
这是真的吗?
我也开始知道"即使主线程程序继续运行".
据我所知:当你启动一个程序时,JVM会创建一个线程来运行你的程序.JVM创建一个用于运行程序的用户线程.该线程称为主线程.从主线程调用类的主要方法.如果程序从主线程生成新线程,它会一直停止,直到最后一个线程死掉.
哪一个是真的?
And*_*s_D 14
我读到了这句话:"主线程必须是完成执行的最后一个线程.当主线程停止时,程序终止."这是真的吗?
不它不是.如果最后一个非守护程序线程已完成,则虚拟机将终止.它不一定是主线程.
简单的例子:
public static void main(String[] args) {
System.out.println("Main thread started");
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Second thread started");
try {
Thread.sleep(2000); // wait two seconds
} catch(Exception e){}
System.out.println("Second thread (almost) finished");
}
}).start();
System.out.println("Main thread (almost) finished");
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
21725 次 |
最近记录: |