特色
一般由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來設置啓動線程爲守護線程。