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


当前回答

java线程中的用户线程与守护线程

守护进程的线程

这些线程在Java中是低优先级的线程,在后台运行以执行诸如垃圾收集之类的任务。Java中的守护线程也是一个服务提供者线程,它向用户线程提供服务。

用户线程

这些线程是高优先级线程。JVM将等待任何用户线程在终止它之前完成它的任务

“请记住用户和守护线程都包装在操作系统线程上”

最近OpenJdk在Loom项目中提出了虚拟线程(它们也是基于用户的),你可以在这里找到更多关于Java虚拟机线程的纤维和延续。

其他回答

以上答案都是好的。下面是一个简单的小代码片段,以说明两者的区别。在setDaemon中分别尝试true和false值。

public class DaemonTest {
    
    public static void main(String[] args) {
        new WorkerThread().start();

        try {
            Thread.sleep(7500);
        } catch (InterruptedException e) {
            // handle here exception
        }

        System.out.println("Main Thread ending") ;
    }

}

class WorkerThread extends Thread {
    
    public WorkerThread() {
        // When false, (i.e. when it's a non daemon thread),
        // the WorkerThread continues to run.
        // When true, (i.e. when it's a daemon thread),
        // the WorkerThread terminates when the main 
        // thread or/and user defined thread(non daemon) terminates.
        setDaemon(true); 
    }
    
    public void run() {
        int count = 0;

        while (true) {
            System.out.println("Hello from Worker "+count++);

            try {
                sleep(5000);
            } catch (InterruptedException e) {
                // handle exception here
            }
        }
    }
}

守护线程

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

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.

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

Java中的守护线程是指运行在后台的线程,主要由JVM创建,用于执行后台任务,如垃圾收集和其他家政任务。

注意事项:

Any thread created by main thread, which runs main method in Java is by default non daemon because Thread inherits its daemon nature from the Thread which creates it i.e. parent Thread and since main thread is a non daemon thread, any other thread created from it will remain non-daemon until explicitly made daemon by calling setDaemon(true). Thread.setDaemon(true) makes a Thread daemon but it can only be called before starting Thread in Java. It will throw IllegalThreadStateException if corresponding Thread is already started and running.

Java中守护线程与非守护线程的区别:

1) JVM在存在之前不会等待任何守护线程完成。

2)当JVM终止时,守护线程与用户线程被区别对待,最终块不被调用,堆栈不受伤,JVM只是退出。

守护线程就像一个普通的线程,除了JVM只会在其他非守护线程不存在时关闭。守护进程线程通常用于为应用程序执行服务。