<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Thread的join方法 關于join官方的解釋是 Waits for this thread to die. 也就是等待一個線程結束。 我們來先來一段代碼來引入join的使用場景(這里使用了java8的IntStream) ```java /** * @program: ThreadDemo * @description: 正常兩個線程交替執行 * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadJoin { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { IntStream.range(1, 1_000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i) ); }, "t1"); t1.start(); IntStream.range(1, 1_000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i) ); } } ``` 運行結果如下 : ![](https://img.kancloud.cn/de/03/de03e91a20189b05be34dfb78ab6d272_1042x305.png) 可以看到正常兩個線程是交替執行的。如果我們想線程t1執行完再執行main線程呢,這里就需要使用join了: ```java /** * @program: ThreadDemo * @description: 使用join方法線程t1結束后才會執行線程main * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadJoin { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { //java8 IntStream IntStream.range(1, 1_000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i) ); }, "t1"); t1.start(); t1.join(); IntStream.range(1, 1_000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i) ); } } ``` 運行結果如下: ![](https://img.kancloud.cn/8a/e4/8ae425a0b4f75301e4515fe31eb1fc7d_696x194.png) 再增加一個子線程,join一下試試: ```java /** * @program: ThreadDemo * @description: 使用join方法線程t1結束后才會執行線程main * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadJoin { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { //java8 IntStream IntStream.range(1, 1_000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i) ); }, "t1"); Thread t2 = new Thread(() -> { IntStream.range(1, 1_000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i) ); }, "t2"); t1.start(); t2.start(); t1.join(); t2.join(); IntStream.range(1, 1_000).forEach(i -> System.out.println(Thread.currentThread().getName() + "->" + i) ); } } ``` ![](https://img.kancloud.cn/c8/52/c8520c15f43869ffca37b76cd00a0819_1146x252.png) ![](https://img.kancloud.cn/67/77/6777f99f13a5e70844c93581a8f3cb44_522x220.png) 結果和我們的預期一樣,兩個子線程執行完之后才執行main線程。 join方法還支持幾個參數: ![](https://img.kancloud.cn/78/19/7819eb678c48017de1ef946442d5f553_1254x172.png) 一個毫秒級別,一個微妙級別,就是最多等待這個線程執行多久。 代碼來測試一下: ```java /** * @program: ThreadDemo * @description: join(long millis), join(long millis, int nanos) * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadJoin2 { public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(() -> { try { System.out.println("t1 is running..."); Thread.sleep(5_000); System.out.println("t1 is done."); } catch (InterruptedException e) { e.printStackTrace(); } }); t1.start(); //只等待t1線程3秒 t1.join(3_000); System.out.println("main thread is running..."); } } ``` 運行效果如下: ![](https://img.kancloud.cn/91/b2/91b242d0651375073a5c2a49b27bc9e9_960x265.gif) 流程如下:程序開始進入t1線程,開始執行t1線程run方法,t1線程run方法打印t1 is running...,然后開始sleep5秒,這時候程序執行到t1.join(3_000);也就是只等待t1線程3秒,等待結束后main線程打印main thread is running...,再過大約2秒,t1sleep結束,打印t1 is done。后面的我們就不測試了,現在引出一個新的問題: 一些嵌入式的HTTP Server,比如jetty,為什么把任務啟動,一會之后會自動掛掉?其實原因很簡單就是在主線程退出之后會把http server掛掉(守護線程),避免占用端口、浪費資源。解決辦法是使用Thread.currentThread().join();。讓當前線程執行,直到當前線程死掉。 測試代碼如下: ```java /** * @program: ThreadDemo * @description: join(long millis), join(long millis, int nanos) * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadJoin2 { public static void main(String[] args) throws InterruptedException { Thread.currentThread().join(); } } ``` 運行效果如下: ![](https://img.kancloud.cn/9b/97/9b977056a4cc486363caa73101aa0732_960x265.gif) 可以看到currentThread一直在等待,thread done....永遠不會輸出出來。 接下來結合一個案例感受一下join在多線程中的使用場景:比如我們啟動多個線程來采集服務器節點的信息,那么我們該如何保證唯一的采集結束時間呢? ```java /** * @program: ThreadDemo * @description: 采集服務器節點的信息的例子。問題:多個線程如何得到唯一的采集結束時間? * @author: hs96.cn@Gmail.com * @create: 2020-09-03 */ public class ThreadJoin3 { public static void main(String[] args) throws InterruptedException { long startTimestamp = System.currentTimeMillis(); // 假設有三臺機器,開啟三個線程。 Thread m1 = new Thread(new CaptureRunnable("M1", 1_000L)); Thread m2 = new Thread(new CaptureRunnable("M2", 2_000L)); Thread m3 = new Thread(new CaptureRunnable("M3", 3_000L)); m1.start(); m2.start(); m3.start(); long endTimestamp = System.currentTimeMillis(); System.out.printf("Save data begin timestamp is %s, end timestamp is %s\n", startTimestamp, endTimestamp); System.out.printf("Spend time is %s", endTimestamp - startTimestamp); } } /** * 采集服務器節點的任務。 */ class CaptureRunnable implements Runnable { // 機器節點的名稱 private String machineName; // 采集花費時間 private long spendTime; public CaptureRunnable(String machineName, long spendTime) { this.machineName = machineName; this.spendTime = spendTime; } @Override public void run() { // do the really capture data. try { Thread.sleep(spendTime); System.out.printf(machineName + " completed data capture at timestamp [%s] and successful.\n", System.currentTimeMillis()); } catch (InterruptedException e) { e.printStackTrace(); } } public String getResult() { return machineName + " finish."; } } ``` 運行效果如下: ![](https://img.kancloud.cn/60/bc/60bc6c591b4a718d153efb7061f32af6_960x265.gif) 可以看到三個線程還沒走完,就提前把時間打印出來了,這個不是我我們想要的效果,那么我們讓三個線程join一下試試: ```java // 假設有三臺機器,開啟三個線程。 Thread m1 = new Thread(new CaptureRunnable("M1", 1_000L)); Thread m2 = new Thread(new CaptureRunnable("M2", 2_000L)); Thread m3 = new Thread(new CaptureRunnable("M3", 3_000L)); m1.start(); m2.start(); m3.start(); m1.join(); m2.join(); m3.join(); ``` 運行效果如下: ![](https://img.kancloud.cn/f7/7e/f77e9e80ccc10081950e8843aaf6fa31_960x265.gif) 這樣就達到我們想要的效果了。
                  <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>

                              哎呀哎呀视频在线观看