淺談java線程join方法使用方法
本博客簡介介紹一下java線程的join方法,join方法是實現(xiàn)線程同步,可以將原本并行執(zhí)行的多線程方法變成串行執(zhí)行的
如圖所示代碼,是并行執(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é)習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用IDEA向Gitee提交SpringBoot項目進行遠程管理
本文主要介紹了使用IDEA向Gitee提交SpringBoot項目進行遠程管理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友們下面隨著小編來一起學(xué)習學(xué)習吧2023-01-01idea打開項目后無法顯示目錄結(jié)構(gòu),只能顯示.iml文件問題
這篇文章主要介紹了idea打開項目后無法顯示目錄結(jié)構(gòu),只能顯示.iml文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08Spring?Lifecycle?和?SmartLifecycle區(qū)別面試精講
這篇文章主要為大家介紹了Spring?Lifecycle和SmartLifecycle的區(qū)別面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10spring?IOC容器管理必須知道的一些操作(基于XML方式)
Spring框架的核心是Spring容器,容器創(chuàng)建對象,將它們裝配在一起,配置它們并管理它們的完整生命周期,下面這篇文章主要給大家介紹了關(guān)于spring?IOC容器管理必須知道的一些操作,需要的朋友可以參考下2022-03-03