Java Thread Join

英文原文:Java Thread Join Example

Java Thread join 方法可以當(dāng)前的線程暫停至你聲明的另一個(gè)線程完全結(jié)束,總共有3種重載方法。

Java Thread join

public final void join(); 這個(gè)方法會使當(dāng)前調(diào)用的線程進(jìn)行等待,直至調(diào)用的這個(gè)方法結(jié)束。如果線程被中斷會拋出InterruptedException。

public final synchronized void join(long millis); 這個(gè)方法會使當(dāng)調(diào)用的線程結(jié)束或者等待你聲明的時(shí)長。等待的時(shí)間由操作系統(tǒng)決定。并不能保證當(dāng)前的線程等待時(shí)間是確定的。

public final synchronized void join(long millis, int nanos); 同上,時(shí)間細(xì)化至毫微秒。

這面這個(gè)例子展示了Thread join 的用方法。這段代碼的主要目的是確保main線程最后一個(gè)執(zhí)行完,且第三個(gè)線程啟必須等第一個(gè)線程執(zhí)行完。

package com.journaldev.threads;

public class ThreadJoinExample {

    public static void main(String[] args) {
        Thread t1 = new Thread(new MyRunnable(), "t1");
        Thread t2 = new Thread(new MyRunnable(), "t2");
        Thread t3 = new Thread(new MyRunnable(), "t3");

        t1.start();

        //start second thread after waiting for 2 seconds or if it's dead
        try {
            t1.join(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t2.start();

        //start third thread only when first thread is dead
        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        t3.start();

        //let all threads finish execution before finishing main thread
        try {
            t1.join();
            t2.join();
            t3.join();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("All threads are dead, exiting main thread");
    }

}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        System.out.println("Thread started:::"+Thread.currentThread().getName());
        try {
            Thread.sleep(4000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Thread ended:::"+Thread.currentThread().getName());
    }

}

上述代碼執(zhí)行完,結(jié)果如下:

Thread started:::t1
Thread started:::t2
Thread ended:::t1
Thread started:::t3
Thread ended:::t2
Thread ended:::t3
All threads are dead, exiting main thread
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,896評論 18 399
  • Java多線程學(xué)習(xí) [-] 一擴(kuò)展javalangThread類 二實(shí)現(xiàn)javalangRunnable接口 三T...
    影馳閱讀 3,117評論 1 18
  • 本文主要講了java中多線程的使用方法、線程同步、線程數(shù)據(jù)傳遞、線程狀態(tài)及相應(yīng)的一些線程函數(shù)用法、概述等。 首先講...
    李欣陽閱讀 2,604評論 1 15
  • 人們往往不知自己漆黑的內(nèi)心萌生著多少謀劃,一個(gè)外來事物不期然地出現(xiàn),一件不幸事情意外的降臨,突然間把那漆黑的計(jì)劃...
    藍(lán)色水杯閱讀 303評論 2 2
  • 前段時(shí)間參加一個(gè)血液透析年會,有一個(gè)老師介紹了一本書,名字是知道做到,作者肯。布蘭佳,保羅。梅耶和迪克。盧瑟。這...
    遮陽的淵虹閱讀 301評論 8 3

友情鏈接更多精彩內(nèi)容