国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

守護線程

2019-11-08 01:43:13
字體:
來源:轉載
供稿:網友

java 中有兩類線程:用戶線程和守護線程。

守護線程的作用

守護線程的作用是為用戶線程的運行提供服務,比如說 GC 線程。

創建一個守護線程

public class DaemonTest_01 { public static void main(String[] args) { Thread thread = new Thread(new DaemonRunner(), "DeamonRunner"); thread.setDaemon(true); // 設置線程為守護線程 thread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.PRintStackTrace(); } } static class DaemonRunner implements Runnable { @Override public void run() { System.out.println("守護線程啟動"); System.out.println("這個線程是守護線程嗎?" + (Thread.currentThread().isDaemon() ? "Yes" : "No")); } }}

輸出:

守護線程啟動 這個線程是守護線程嗎?Yes

關鍵在于 thread.setDaemon(true) 這段代碼。

守護線程的特點

特點一: setDaemon(true) 必須在 start() 之前,否則會有異常。你不能把正在運行的常規線程設置為守護線程。

特點二: 守護線程存在的目的是為用戶線程提供服務,因此如果用戶線程全部撤離,那么守護線程也就沒什么存在的必要了,所以虛擬機也就退出了。所以守護線程中的 finally 塊不一定會執行。

下面舉例驗證:

public class DaemonTest_02 { public static void main(String[] args) { Thread thread = new Thread(new DaemonRunner(), "DeamonRunner"); thread.setDaemon(true); thread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } static class DaemonRunner implements Runnable { @Override public void run() { try { System.out.println("守護線程啟動"); System.out.println("這個線程是守護線程嗎?" + (Thread.currentThread().isDaemon() ? "Yes" : "No")); Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } finally { System.out.println("進入 finally 方法"); } } }}

可能的一種輸出:

守護線程啟動 這個線程是守護線程嗎?Yes

main 方法執行完畢后虛擬機中已經沒有用戶線程了,虛擬機需要退出,所有守護線程立即終止,但是 finally 塊并沒有執行。因此在構建守護線程時,不能依靠 finally 塊中的內容來確保執行關閉或清理資源的邏輯。

特點三: 守護線程創建的新線程也是守護線程。

舉例說明:

public class DaemonTest_03 { public static void main(String[] args) { Thread thread = new Thread(new DaemonRunner(), "DeamonRunner"); thread.setDaemon(true); thread.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } static class DaemonRunner implements Runnable { @Override public void run() { System.out.println("守護線程啟動"); System.out.println("這個線程是守護線程嗎:" + (Thread.currentThread().isDaemon()?"Yes":"No")); try { Thread thread = new Thread(new Runner(), "Runner"); // thread.setDaemon(true); 注意這里并沒有明確設置新線程為守護線程 thread.start(); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } static class Runner implements Runnable { @Override public void run() { System.out.println("新線程啟動"); System.out.println("這個新線程是守護線程嗎:" + (Thread.currentThread().isDaemon()?"Yes":"No")); } }}

輸出:

守護線程啟動 這個線程是守護線程嗎:Yes 新線程啟動 這個新線程是守護線程嗎:Yes


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上高县| 黔西| 安宁市| 泗阳县| 当阳市| 浦江县| 怀安县| 沂南县| 瑞丽市| 南部县| 嘉黎县| 凤台县| 松溪县| 汤原县| 林甸县| 治多县| 金川县| 宁武县| 剑阁县| 扎兰屯市| 西城区| 双牌县| 曲麻莱县| 乌审旗| 黄梅县| 铜川市| 津南区| 波密县| 乌兰察布市| 临清市| 石台县| 沂源县| 浦江县| 府谷县| 遵义市| 顺昌县| 汕尾市| 凤山市| 凤山市| 若尔盖县| 宾阳县|