特色
一般由JVM启动
运行在后台处理任务,好比垃圾回收等
用户启动线程执行结束或者JVM结束时,会等待全部的非守护线程执行结束,可是不会由于守护线程的存在而影响关闭。
判断线程是否为守护线程
判断一个线程是否为守护线程,主要依据以下的内容java
/* 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; }
下面咱们进行一些简单的代码,验证一些关于守护线程的特性和一些猜想。架构
辅助方法
打印线程信息的方法,输出线程的组,是否为守护线程以及对应的优先级。ide
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(); } }
验证普通的(非守护线程)线程会影响进程(JVM)退出this
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)); }
获取输出日志spa
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不等待JVM启动的守护线程(Reference Handler,Signal Dispatcher等)执行结束就退出。code
这里咱们再次用一段代码验证一下JVM不等待用户启动的守护线程结束就退出的事实。orm
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没有等待守护线程结束就退出了。
注意:在此我向你们推荐一个架构学习交流裙。交流学习裙号:687810532,里面会分享一些资深架构师录制的视频录像
新的线程是否初始为守护线程,取决于启动该线程的线程是否为守护线程。 守护线程默认启动的线程为守护线程,非守护线程启动的线程默认为非守护线程。 主线程(非守护线程)启用一个守护线程,须要调用Thread.setDaemon来设置启动线程为守护线程。