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


当前回答

守护线程是在进程的其他非守护线程仍在运行时在后台运行的线程。因此,当所有非守护进程线程完成时,守护进程线程将终止。非守护进程线程的一个例子是运行Main的线程。 通过在线程启动之前调用setDaemon()方法,将线程设置为守护进程

更多参考:Java中的守护进程线程

其他回答

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

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

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

守护进程的线程

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

用户线程

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

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

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

传统上,UNIX中的守护进程是那些不断在后台运行的进程,就像Windows中的服务一样。

Java中的守护线程是不阻止JVM退出的线程。特别是当只剩下守护线程时,JVM将退出。您可以通过调用线程上的setDaemon()方法来创建一个。

读取Daemon线程。

答案已经有很多了;然而,也许我可以更清楚地解释这一点,因为当我阅读Daemon Threads时,最初,我有一种感觉,我很好地理解了它;然而,在玩了它并调试了一下之后,我发现了一个奇怪的行为。

我被教导:

如果我想让线程在主线程有序完成执行后立即死亡,我应该将其设置为Diamond。

我尝试了什么:

我从主线程中创建了两个线程,我只将其中一个设置为菱形; 在主线程有序完成执行后,那些新创建的线程都没有退出,但我预计,守护线程应该已经退出; 我浏览了许多博客和文章,到目前为止,我找到的最好、最清晰的定义来自《Java并发实践》一书,它非常清楚地指出:

7.4.2守护线程

Sometimes you want to create a thread that performs some helper function but you don’t want the existence of this thread to prevent the JVM from shutting down. This is what daemon threads are for. Threads are divided into two types: normal threads and daemon threads. When the JVM starts up, all the threads it creates (such as garbage collector and other housekeeping threads) are daemon threads, except the main thread. When a new thread is created, it inherits the daemon status of the thread that created it, so by default any threads created by the main thread are also normal threads. Normal threads and daemon threads differ only in what happens when they exit. When a thread exits, the JVM performs an inventory of running threads, and if the only threads that are left are daemon threads, it initiates an orderly shutdown. When the JVM halts, any remaining daemon threads are abandoned— finally blocks are not executed, stacks are not unwound—the JVM just exits. Daemon threads should be used sparingly—few processing activities can be safely abandoned at any time with no cleanup. In particular, it is dangerous to use daemon threads for tasks that might perform any sort of I/O. Daemon threads are best saved for “housekeeping” tasks, such as a background thread that periodically removes expired entries from an in-memory cache.

下面是一个示例,用于测试在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