JVM退出后,守护程序线程如何生存? [英] How does daemon thread survive after JVM exits?

查看:382
本文介绍了JVM退出后,守护程序线程如何生存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关Java的 setDaemon()方法的文档,当我读到JVM退出而不等待守护程序线程完成时,我感到很困惑。



但是,由于本质上守护程序线程是Java Thread ,这可能依赖于在JVM上运行来实现其功能,如果JVM在守护程序线程完成之前退出,守护程序线程如何能够存活?

解决方案

他们无法生存。除了守护进程之外的所有线程都已经死亡,JVM将退出。



当你启动应用程序时,JVM将启动一个非守护进程线程到运行你的静态main方法。



一旦main方法退出,这个主线程就会死掉,如果你没有产生其他非守护进程线程,JVM将退出。 / p>

如果你开始了另一个线程,JVM将不会退出,它将等待所有非守护进程线程在退出之前死掉。



如果你产生的那个线程正在做一些重要的事情,这绝对是正确的事情,但是你经常会遇到一些并不重要的线程,也许他们正在听一些可能会或可能会发生的外部事件。不会发生。



因此,理论上,您应该在某处放置一些代码来阻止您生成的所有线程以允许JVM退出。



由于这很容易出错,因此将这些非重要线程标记为守护进程会更容易。如果它们被标记为这样,JVM将不会在退出之前等待它们死亡,当主线程(未标记为守护进程)已经死亡时,JVM将退出并终止这些线程。



要把它放在代码中,它是这样的:

  public class Spawner {
public static void main(String [] args){
Thread t = new Thread(new Runnable(){
public void run(){
while(true){
System.out.println(我还活着);
}
}
});
t.start();
//尝试取消注释/评论此行
// t.setDaemon(true);
System.out.println(主线程已完成);
}
}

(我没有测试过这段代码,写了它这里直接,所以它可能包含愚蠢的错误。)



当使用注释行运行此代码时,线程不是deamon,所以即使你的main方法已经完成,你会继续让控制台泛滥,直到你用CTRL + C来阻止它。也就是说,JVM不会退出。



如果你取消注释该行,那么该线程就是一个守护进程,并且在main方法完成后不久,该线程将是杀死,JVM将退出,无需CTRL + C.


I am reading docs about Java's setDaemon() method, and got confused when I read that JVM exits without waiting for daemon threads to finish.

However, since essentially daemon threads are Java Thread's, which presumably rely on running on JVM to achieve its functionalities, how do daemon threads even survive if JVM exits before the daemon thread finishes?

解决方案

They don't survive. The JVM will exit when all the threads, except the daemon ones, have died.

When you start your application, the JVM will start a single, non-daemon thread to run your static main method.

Once the main method exits, this main thread will die, and if you spawned no other non-daemon thread, the JVM will exit.

If however you started another thread, the JVM will not exit, it will wait for all the non-daemon threads to die before exiting.

If that thread you spawned is doing something vital, this is absolutely the right thing to do, however often you have some threads that are not that vital, maybe they are listening to some external event that may or may not happen.

So, in theory, you should place some code somewhere to stop all the threads you spawned to allow the JVM to exit.

Since this is error prone, it's way easier to mark such non-vital threads as daemons. If they are marked as such, the JVM will not wait for them to die before exiting, the JVM will exit and kill those threads when the "main threads" (those not marked as daemon) have died.

To put it in code, it's something like this :

public class Spawner {
  public static void main(String[] args) {
    Thread t = new Thread(new Runnable() {
      public void run() {
        while (true) {
          System.out.println("I'm still alive");
        }
      }
    });
    t.start();
    // Try uncommenting/commenting this line
    // t.setDaemon(true);
    System.out.println("Main thread has finished");
  }
}

(I haven't tested this code, wrote it here directly, so it could contain stupid mistakes).

When running this code with the line commented, the thread is not deamon, so even if your main method has finished, you'll keep on having the console flooded until you stop it with CTRL+C. That is, the JVM will not exit.

If you uncomment the line, then the thread is a daemon, and soon after the main method has finished, the thread will be killed and the JVM will exit, without the need for CTRL+C.

这篇关于JVM退出后,守护程序线程如何生存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆