Daemon thread in java can be useful to run some tasks in background. When we create a thread in java, by default it’s a user thread and if it’s running JVM will not terminate the program.
Java中的守护程序线程对于在后台运行某些任务很有用。 当我们在java中创建线程时 ,默认情况下它是用户线程,并且如果它正在运行,JVM不会终止程序。
(Daemon thread in java)
When a thread is marked as daemon thread, JVM doesn’t wait it to finish to terminate the program. As soon as all the user threads are finished, JVM terminates the program as well as all the associated daemon threads.
当一个线程被标记为守护程序线程时,JVM不会等待它完成以终止程序。 一旦所有用户线程完成,JVM就会终止程序以及所有关联的守护程序线程。
Thread.setDaemon(true)
is used to create a daemon thread in java. This method should be invoked before the thread is started otherwise it will throw IllegalThreadStateException
.
Thread.setDaemon(true)
用于在Java中创建守护程序线程。 应该在启动线程之前调用此方法,否则它将抛出IllegalThreadStateException
。
We can check if a thread is daemon thread or not by calling isDaemon()
method on it.
我们可以通过在线程上调用isDaemon()
方法来检查线程是否为守护线程。
Another point is that when a thread is started, it inherits the daemon status of it’s parent thread.
另一点是,当线程启动时,它将继承其父线程的守护程序状态。
(Daemon Thread in Java Example)
Let’s see a small example of daemon thread in java.
让我们看一下Java中守护线程的一个小例子。
package com.journaldev.threads;
public class JavaDaemonThread {
public static void main(String[] args) throws InterruptedException {
Thread dt = new Thread(new DaemonThread(), "dt");
dt.setDaemon(true);
dt.start();
//continue program
Thread.sleep(30000);
System.out.println("Finishing program");
}
}
class DaemonThread implements Runnable{
@Override
public void run() {
while(true){
processSomething();
}
}
private void processSomething() {
try {
System.out.println("Processing daemon thread");
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
When we execute above daemon thread program, JVM creates first user thread with main()
method and then a daemon thread.
当我们执行上述守护程序线程程序时,JVM首先使用main()
方法创建用户线程,然后再创建守护程序线程。
When main method is finished, the program terminates and daemon thread is also shut down by JVM.
当main方法完成时,程序终止,并且JVM也关闭了守护程序线程。
Below image shows the output of the above program.
下图显示了以上程序的输出。
If we don’t set the “dt” thread to be run as daemon thread, the program will never terminate even after main thread is finished it’s execution. Notice that DaemonThread
is having a while true loop with thread sleep, so it will never terminate on it’s own.
如果我们不将“ dt”线程设置为守护程序线程运行,则即使主线程完成执行,程序也永远不会终止。 请注意, DaemonThread
线程睡眠有一个true循环,因此它永远不会自行终止。
Try to comment the statement to set thread as daemon thread and run the program. You will notice that program runs indefinitely and you will have to manually quit it.
尝试注释该语句以将线程设置为守护程序线程并运行程序。 您会注意到该程序会无限期运行,并且您必须手动退出它。
(Daemon Thread Usage)
Usually we create a daemon thread for functionalities that are not critical to system. For example logging thread or monitoring thread to capture the system resource details and their state. If you are not okay will a thread being terminated, don’t create it as a daemon thread.
通常,我们为对系统不重要的功能创建一个守护线程。 例如,日志记录线程或监视线程可捕获系统资源详细信息及其状态。 如果您不满意,线程将被终止,请不要将其创建为守护线程。
Also it’s better to avoid daemon threads for IO operations because it can cause resource leak when program just terminates and resources are not closed properly.
另外,最好避免使用守护程序线程进行IO操作,因为当程序刚刚终止且资源未正确关闭时,它可能导致资源泄漏。
Reference: API Doc
参考: API文档