有人能告诉我Java中的守护线程是什么吗?


当前回答

再讲一点(参考:Java并发实践)

当创建一个新线程时,它将继承其守护进程状态 的父母。 当所有非守护进程线程完成时,JVM停止,并放弃所有剩余的守护进程线程: 最后,块不执行, 栈不会被解开——JVM只是退出。 由于这个原因,应该谨慎使用守护线程,将它们用于可能执行任何类型的I/O的任务是危险的。

其他回答

守护线程是在程序完成但线程仍在运行时不阻止JVM退出的线程。守护进程线程的一个例子是垃圾收集。

您可以使用setDaemon(boolean)方法在线程启动之前更改线程守护进程属性。

Daemon thread is like daemon process which is responsible for managing resources,a daemon thread is created by the Java VM to serve the user threads. example updating system for unix,unix is daemon process. child of daemon thread is always daemon thread,so by default daemon is false.you can check thread as daemon or user by using "isDaemon()" method. so daemon thread or daemon process are basically responsible for managing resources. for example when you starting jvm there is garbage collector running that is daemon thread whose priority is 1 that is lowest,which is managing memory. jvm is alive as long as user thread is alive,u can not kill daemon thread.jvm is responsible to kill daemon threads.

下面是一个示例,用于测试在jvm由于不存在用户线程而退出时守护线程的行为。

请注意下面输出的倒数第二行,当主线程退出时,守护线程也死了,并且没有在finally块中打印finally executed9语句。这意味着如果由于不存在用户线程而导致JVM退出,守护线程finally块内关闭的任何i/o资源都不会被关闭。

public class DeamonTreadExample {

public static void main(String[] args) throws InterruptedException {

    Thread t = new Thread(() -> {
        int count = 0;
        while (true) {
            count++;
            try {
                System.out.println("inside try"+ count);
                Thread.currentThread().sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                System.out.println("finally executed"+ count);
            }
        }
    });
    t.setDaemon(true);
    t.start();

    Thread.currentThread().sleep(10000);
    System.out.println("main thread exited");
  }
}

输出

inside try1
finally executed1
inside try2
finally executed2
inside try3
finally executed3
inside try4
finally executed4
inside try5
finally executed5
inside try6
finally executed6
inside try7
finally executed7
inside try8
finally executed8
inside try9
finally executed9
inside try10
main thread exited

守护线程和用户线程。通常程序员创建的所有线程都是用户线程(除非你指定它为守护线程或者你的父线程是守护线程)。用户线程通常用于运行我们的程序代码。除非所有用户线程都终止,否则JVM不会终止。

守护线程

在后台运行的线程称为守护线程。

Daemon线程示例:

垃圾收集器。 信号分配器。

守护线程的目标:

守护进程线程的主要目标是为非守护进程线程提供支持。

有关守护线程的其他信息:

通常,守护线程在MIN_PRIORITY中运行,但是,也可以使用MAX_PRIORITY运行守护线程。 示例:当需要额外的内存时,GC通常以MIN_PRIORITY优先级运行。JVM将GC的优先级从MIN_PRIORITY增加到MAX_PRIORITY。


一旦线程已经启动,就不能将其从守护线程更改为非守护线程,这会导致IllegalThreadStateException异常。 例子: public static void main(String[] args) { Thread.currentThread () .setDaemon(真正的); } 输出: java.lang.IllegalThreadStateException线程异常 在java.base / java.lang.Thread.setDaemon (Thread.java: 1403)


If we are branching off a thread, the child thread inherits the nature of the parent thread. If the parent thread is a non-daemon thread automatically the child thread will be non-daemon as well and if the parent thread is a daemon, the child thread will be a daemon as well. class Scratch { public static void main(String[] args) { CustomThread customThread = new CustomThread(); customThread.start(); } } class CustomThread extends Thread{ @Override public void run() { System.out.println(currentThread().isDaemon()); } } Output: false


When the last non-daemon thread terminates, all the daemon threads get terminated automatically. class Scratch { public static void main(String[] args) { System.out.println("Main Thread Started."); CustomThread customThread = new CustomThread(); customThread.setDaemon(true); customThread.start(); System.out.println("Main Thread Finished."); } } class CustomThread extends Thread{ @Override public void run() { System.out.println("Custom Thread Started."); try { sleep(2000); } catch (InterruptedException ignore) {} System.out.println("Custom Thread Finished."); //Won't get executed. } } Output: Main Thread Started. Main Thread Finished. Custom Thread Started.