<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Daemon線程的創建以及使用場景分析 首先我們先創建一個普通的線程,我們知道main方法實際也是個線程,讓他們交替打印文字: ```java /** * @program: ThreadDemo * @description: 守護線程demo * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class DaemonThread { public static void main(String[] args) throws InterruptedException { Thread t = new Thread() { @Override public void run() { try { System.out.println(Thread.currentThread().getName() + " running"); Thread.sleep(3_000); System.out.println(Thread.currentThread().getName() + " done"); } catch (InterruptedException e) { e.printStackTrace(); } } }; t.start(); System.out.println(Thread.currentThread().getName()); } } ``` 執行效果如下: ![](https://img.kancloud.cn/b3/27/b3271adb7ee0f9c31f750aa9b5c27864_960x638.gif) 接下來把Thread t 設置為守護線程: ```java /** * @program: ThreadDemo * @description: 守護線程demo * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class DaemonThread { public static void main(String[] args) throws InterruptedException { Thread t = new Thread() { @Override public void run() { try { System.out.println(Thread.currentThread().getName() + " running"); Thread.sleep(5_000); System.out.println(Thread.currentThread().getName() + " done"); } catch (InterruptedException e) { e.printStackTrace(); } } }; t.setDaemon(true); t.start(); Thread.sleep(3_000); System.out.println(Thread.currentThread().getName()); } } ``` 執行效果如下: ![](https://img.kancloud.cn/59/bc/59bc477e103a354c9fb5457878f1e308_960x638.gif) 見這時當main線程一結束,我們的t線程就退出了,因為沒打印Thread-0 done。其實守護線程的使用場景還是有很多的,比如:當客戶端與服務器端建立一個TCP的長鏈接,然后當連接建立之后就創建一個線程來給服務器發送心跳包以便服務器能監聽客戶端的網絡狀態,這時如果連接斷開了那這個心跳線程也得跟著斷開,接下來用代碼模擬一下這個操作: ```java /** * @program: ThreadDemo * @description: 模擬客戶端與服務器端建立一個TCP的長鏈接發送心跳包 * @author: hs96.cn@Gmail.com * @create: 2020-08-28 */ public class DaemonThread2 { public static void main(String[] args) throws InterruptedException { Thread thread = new Thread(() -> { System.out.println("tcp connection..."); Thread innerThread = new Thread(() -> { try { while (true) { System.out.println("packet sending..."); Thread.sleep(1_000); } } catch (InterruptedException e) { e.printStackTrace(); } }, "innerThread"); innerThread.setDaemon(true); innerThread.start(); try { Thread.sleep(4_000); System.out.println("tcp Disconnect..."); } catch (InterruptedException e) { e.printStackTrace(); } }, "thread"); thread.start(); } } ``` 運行效果如下: ![](https://img.kancloud.cn/1e/fe/1efe3f839d6382022e2816e7a8aaae04_960x638.gif) 可以看到:我們將發送心跳包的線程設置為守護線程,這個線程隨著tcp連接的線程關閉而關閉了。 有一點值得一提:**setDaemon 必須放在start前**。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看