当我们在Java中创建线程的时候,这个线程在默许的情况下是1个用户线程,并且,如果这个线程在运行,那末JVM就不会终结这个利用。和用户线程不同,当1个线程被标记为守护线程的时候,JVM在用户线程结束的时候,是不会延续等待守护线程结束的,而是直接结束程序,并且结束程序中相干的守护线程。
Thread.setDaemon(true)
方法可以用来将线程置为守护线程,参考下面的例子:
package com.sapphire.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();
}
}
}
当履行上面的程序时,JVM会包括main方法这个线程,而后在main方法中创建1个用户线程,然后是变成了守护线程。当main函数结束的时候,程序就会结束,而且JVM也会同时关闭掉守护线程。
程序的输出以下:
Processing daemon thread
Processing daemon thread
Processing daemon thread
Processing daemon thread
Processing daemon thread
Processing daemon thread
Finishing program
但是,如果我们未将线程置为守护线程的话,程序就永久都不会结束,就算main函数的线程履行终了,程序依然会继续下去。
通常来讲,我们创建1个守护线程为了1些跟系统不那末相干的功能。举例来讲,日志线程或监控系统资源状态的线程等。
守护线程的配置必须在线程启动之前,参考Javadoc
/** * Marks this thread as either a {@linkplain #isDaemon daemon} thread * or a user thread. The Java Virtual Machine exits when the only * threads running are all daemon threads. * * <p> This method must be invoked before the thread is started. * * @param on * if {@code true}, marks this thread as a daemon thread * * @throws IllegalThreadStateException * if this thread is {@linkplain #isAlive alive} * * @throws SecurityException * if {@link #checkAccess} determines that the current * thread cannot modify this thread */ public final void setDaemon(boolean on) { checkAccess(); if (isAlive()) { throw new IllegalThreadStateException(); } daemon = on; }