本博客簡介介紹一下java線程的join方法,join方法是實現(xiàn)線程同步,可以將原本并行執(zhí)行的多線程方法變成串行執(zhí)行的
成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設(shè)計、重慶網(wǎng)站建設(shè)公司、微信開發(fā)、微信平臺小程序開發(fā)、集團成都企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:茶樓設(shè)計等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致表揚!
如圖所示代碼,是并行執(zhí)行的
public class ThreadTest { //private static final Long count = 10000L; public static void main(String[] args){ long base = System.currentTimeMillis(); try { ThreadJoin t1 = new ThreadJoin("線程1"); ThreadJoin t2 = new ThreadJoin("線程2"); //t1.join(); t1.start(); t1.join(); t2.start(); } catch (Exception e) { e.printStackTrace(); } long time = System.currentTimeMillis() - base; System.out.println("執(zhí)行時間:"+time); } } class ThreadJoin extends Thread{ private static final Long count = 10L; public ThreadJoin(String name){ super(name); } @Override public void run() { //super.run(); for(int i = 1; i <= count; i ++){ System.out.println(this.getName()+":"+i); } } }
打印出來的信息,都是這樣的
執(zhí)行時間:0
線程1:1
線程2:1
線程2:2
線程2:3
線程2:4
線程2:5
線程2:6
線程2:7
線程2:8
線程2:9
線程2:10
線程1:2
線程1:3
線程1:4
線程1:5
線程1:6
線程1:7
線程1:8
線程1:9
線程1:10
要實現(xiàn)串行執(zhí)行,可以加上join方法,實現(xiàn)線程1執(zhí)行完成后才開始執(zhí)行線程2,也就是串行執(zhí)行
public class ThreadTest { //private static final Long count = 10000L; public static void main(String[] args){ long base = System.currentTimeMillis(); try { ThreadJoin t1 = new ThreadJoin("線程1"); ThreadJoin t2 = new ThreadJoin("線程2"); //t1.join(); t1.start(); t1.join(); t2.start(); } catch (Exception e) { e.printStackTrace(); } long time = System.currentTimeMillis() - base; System.out.println("執(zhí)行時間:"+time); } } class ThreadJoin extends Thread{ private static final Long count = 10L; public ThreadJoin(String name){ super(name); } @Override public void run() { //super.run(); for(int i = 1; i <= count; i ++){ System.out.println(this.getName()+":"+i); } } }
線程1:1
線程1:2
線程1:3
線程1:4
線程1:5
線程1:6
線程1:7
線程1:8
線程1:9
線程1:10
執(zhí)行時間:0
線程2:1
線程2:2
線程2:3
線程2:4
線程2:5
線程2:6
線程2:7
線程2:8
線程2:9
線程2:10
從執(zhí)行結(jié)果看,已經(jīng)是串行執(zhí)行線程
所以上面的例子是調(diào)了現(xiàn)場1的join方法,也就是說要先執(zhí)行完成線程1,然后才執(zhí)行main主線程
join方法的作用是,舉個例子,在A線程里調(diào)B線程的join方法時,要先B線程執(zhí)行完成,然后才會繼續(xù)執(zhí)行A線程
ok,上面調(diào)join方法是不加參數(shù)的,也可以加上參數(shù),比如線程A.join(10);,就是說線程A執(zhí)行10s后,繼續(xù)執(zhí)行B線程
注意:join時間參數(shù)缺省的情況,默認是0,也就是說join()等同于join(0);
/** * Waits for this thread to die. * * <p> An invocation of this method behaves in exactly the same * way as the invocation * * <blockquote> * {@linkplain #join(long) join}{@code (0)} * </blockquote> * * @throws InterruptedException * if any thread has interrupted the current thread. The * <i>interrupted status</i> of the current thread is * cleared when this exception is thrown. */ public final void join() throws InterruptedException { join(0); }
Thread類里的源碼,可以看出默認賦值為0,然后這個0是什么意思?0不是表示執(zhí)行0s,而是表示要A線程執(zhí)行完成才繼續(xù)執(zhí)行B線程的意思
ok,然后為什么調(diào)用了join方法就可以實現(xiàn)線程同步?我們簡單看一下代碼:
public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; //執(zhí)行時間必須為正數(shù) if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } //執(zhí)行時間為0或者缺省情況 if (millis == 0) { while (isAlive()) {//表示線程還沒執(zhí)行好 wait(0);//調(diào)用線程的wait方法 } } else {//執(zhí)行時間大于0的情況 while (isAlive()) { long delay = millis - now;//循環(huán)計算延期時間 if (delay <= 0) { break; } wait(delay);//同樣調(diào)用線程的wait方法 now = System.currentTimeMillis() - base; } } }
ok,看了一下源碼,還是比較容易理解的,其實就是調(diào)用了現(xiàn)場wait方法實現(xiàn)線程同步的
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
網(wǎng)站欄目:淺談java線程join方法使用方法
文章來源:http://aaarwkj.com/article42/gjocec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、做網(wǎng)站、響應(yīng)式網(wǎng)站、手機網(wǎng)站建設(shè)、網(wǎng)站維護、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)