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


当前回答

Java有一种特殊的线程,称为守护线程。

非常低的优先级。 仅在同一程序没有其他线程正在运行时执行。 当守护线程结束时,JVM结束程序,完成这些线程 程序中运行的唯一线程。

守护线程用于什么?

通常用作普通线程的服务提供者。 通常有一个等待服务请求或执行线程任务的无限循环。 他们不能做重要的工作。(因为我们不知道它们什么时候会有CPU时间,如果没有任何其他线程在运行,它们可以在任何时间完成。)

这类线程的一个典型示例是Java垃圾收集器。

有更多的…

在调用start()方法之前,只能调用setDaemon()方法。线程运行后,就不能修改它的守护进程状态。 使用isDaemon()方法检查线程是守护线程还是用户线程。

其他回答

答案已经有很多了;然而,也许我可以更清楚地解释这一点,因为当我阅读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.

在Java中,守护线程是一种不阻止Java虚拟机(JVM)退出的线程类型。 守护线程的主要目的是执行后台任务,特别是一些例行的周期性任务或工作。JVM退出时,守护进程线程也会死亡。

通过设置thread. setdaemon (true),线程变成守护线程。但是,您只能在线程启动之前设置这个值。

守护线程

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

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退出的线程。守护进程线程的一个例子是垃圾收集。

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

Daemon threads are those threads which provide general services for user threads (Example : clean up services - garbage collector) Daemon threads are running all the time until kill by the JVM Daemon Threads are treated differently than User Thread when JVM terminates , finally blocks are not called JVM just exits JVM doesn't terminates unless all the user threads terminate. JVM terminates if all user threads are dies JVM doesn't wait for any daemon thread to finish before existing and finally blocks are not called If all user threads dies JVM kills all the daemon threads before stops When all user threads have terminated, daemon threads can also be terminated and the main program terminates setDaemon() method must be called before the thread's start() method is invoked Once a thread has started executing its daemon status cannot be changed To determine if a thread is a daemon thread, use the accessor method isDaemon()