在之前的 《详解JVM如何处理异常》 提到了守护线程,当时没有详细解释,所以打算放到今天来解释说明一下JVM守护线程的内容。
判断一个线程是否为守护线程,主要依据如下的内容
/* Whether or not the thread is a daemon thread. */
private boolean daemon = false;
/**
* Tests if this thread is a daemon thread.
*
* @return <code>true</code> if this thread is a daemon thread;
* <code>false</code> otherwise.
* @see #setDaemon(boolean)
*/
public final boolean isDaemon() {
return daemon;
}
下面我们进行一些简单的代码,验证一些关于守护线程的特性和一些猜测。
打印线程信息的方法,输出线程的组,是否为守护线程以及对应的优先级。
private static void dumpAllThreadsInfo() {
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
for(Thread thread: threadSet) {
System.out.println("dumpAllThreadsInfo thread.name=" + thread.getName()
+ ";group=" + thread.getThreadGroup()
+ ";isDaemon=" + thread.isDaemon()
+ ";priority=" + thread.getPriority());
}
}
线程睡眠的方法
private static void makeThreadSleep(long durationInMillSeconds) {
try {
Thread.sleep(durationInMillSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private static void testNormalThread() {
long startTime = System.currentTimeMillis();
new Thread("NormalThread") {
@Override
public void run() {
super.run();
//保持睡眠,确保在执行dumpAllThreadsInfo时,该线程不会因为退出导致dumpAllThreadsInfo无法打印信息。
makeThreadSleep(10 * 1000);
System.out.println("startNormalThread normalThread.time cost=" + (System.currentTimeMillis() - startTime));
}
}.start();
//主线程暂定3秒,确保子线程都启动完成
makeThreadSleep(3 * 1000);
dumpAllThreadsInfo();
System.out.println("MainThread.time cost = " + (System.currentTimeMillis() - startTime));
}
获取输出日志
dumpAllThreadsInfo thread.name=Signal Dispatcher;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=9 dumpAllThreadsInfo thread.name=Attach Listener;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=9 dumpAllThreadsInfo thread.name=Monitor Ctrl-Break;group=java.lang.ThreadGroup[name=main,maxpri=10];isDaemon=true;priority=5 dumpAllThreadsInfo thread.name=Reference Handler;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=10 dumpAllThreadsInfo thread.name=main;group=java.lang.ThreadGroup[name=main,maxpri=10];isDaemon=false;priority=5 dumpAllThreadsInfo thread.name=NormalThread;group=java.lang.ThreadGroup[name=main,maxpri=10];isDaemon=false;priority=5 dumpAllThreadsInfo thread.name=Finalizer;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=8 MainThread.time cost = 3009 startNormalThread normalThread.time cost=10003 Process finished with exit code 0 结束进程
我们根据上面的日志,我们可以发现
startNormalThread normalThread.time cost=10003 Process finished with exit code 0
以上日志可以验证进程是在我们启动的子线程结束之后才退出的。
其实上面的例子也可以验证JVM不等待JVM启动的守护线程(Reference Handler,Signal Dispatcher等)执行结束就退出。
这里我们再次用一段代码验证一下JVM不等待用户启动的守护线程结束就退出的事实。
private static void testDaemonThread() {
long startTime = System.currentTimeMillis();
Thread daemonThreadSetByUser = new Thread("daemonThreadSetByUser") {
@Override
public void run() {
makeThreadSleep(10 * 1000);
super.run();
System.out.println("daemonThreadSetByUser.time cost=" + (System.currentTimeMillis() - startTime));
}
};
daemonThreadSetByUser.setDaemon(true);
daemonThreadSetByUser.start();
//主线程暂定3秒,确保子线程都启动完成
makeThreadSleep(3 * 1000);
dumpAllThreadsInfo();
System.out.println("MainThread.time cost = " + (System.currentTimeMillis() - startTime));
}
上面的结果得到的输出日志为
dumpAllThreadsInfo thread.name=Signal Dispatcher;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=9 dumpAllThreadsInfo thread.name=Attach Listener;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=9 dumpAllThreadsInfo thread.name=Monitor Ctrl-Break;group=java.lang.ThreadGroup[name=main,maxpri=10];isDaemon=true;priority=5 dumpAllThreadsInfo thread.name=Reference Handler;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=10 dumpAllThreadsInfo thread.name=main;group=java.lang.ThreadGroup[name=main,maxpri=10];isDaemon=false;priority=5 dumpAllThreadsInfo thread.name=daemonThreadSetByUser;group=java.lang.ThreadGroup[name=main,maxpri=10];isDaemon=true;priority=5 dumpAllThreadsInfo thread.name=Finalizer;group=java.lang.ThreadGroup[name=system,maxpri=10];isDaemon=true;priority=8 MainThread.time cost = 3006 Process finished with exit code 0
我们可以看到,上面的日志没有类似daemonThreadSetByUser.time cost=的信息。可以确定JVM没有等待守护线程结束就退出了。
注意:
有一种传言为守护线程的优先级要低,然而事实是
感兴趣的可以自己验证一下(其实上面的代码已经有验证了)