前言
在Java中有兩類線程:User Thread(用戶線程)、Daemon Thread(守護線程)
用個比較通俗的比如,任何一個守護線程都是整個JVM中所有非守護線程的保姆:
只要當前JVM實例中尚存在任何一個非守護線程沒有結束,守護線程就全部工作;只有當最后一個非守護線程結束時,守護線程隨著JVM一同結束工作。
Daemon的作用是為其他線程的運行提供便利服務,守護線程最典型的應用就是 GC (垃圾回收器),它就是一個很稱職的守護者。
在之前的《詳解JVM如何處理異?!诽岬搅耸刈o線程,當時沒有詳細解釋,所以打算放到今天來解釋說明一下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(); } }
驗證普通的(非守護線程)線程會影響進程(JVM)退出
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不等待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 = 3006Process finished with exit code 0
我們可以看到,上面的日志沒有類似daemonThreadSetByUser.time cost=的信息??梢源_定JVM沒有等待守護線程結束就退出了。
注意:
關于Priority與守護線程的關系
有一種傳言為守護線程的優先級要低,然而事實是
感興趣的可以自己驗證一下(其實上面的代碼已經有驗證了)
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。