淺談java線程join方法使用方法
本博客簡(jiǎn)介介紹一下java線程的join方法,join方法是實(shí)現(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í)行時(shí)間:"+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); } } }
打印出來(lái)的信息,都是這樣的
執(zhí)行時(shí)間: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
要實(shí)現(xiàn)串行執(zhí)行,可以加上join方法,實(shí)現(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í)行時(shí)間:"+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í)行時(shí)間: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)場(chǎng)1的join方法,也就是說(shuō)要先執(zhí)行完成線程1,然后才執(zhí)行main主線程
join方法的作用是,舉個(gè)例子,在A線程里調(diào)B線程的join方法時(shí),要先B線程執(zhí)行完成,然后才會(huì)繼續(xù)執(zhí)行A線程
ok,上面調(diào)join方法是不加參數(shù)的,也可以加上參數(shù),比如線程A.join(10);,就是說(shuō)線程A執(zhí)行10s后,繼續(xù)執(zhí)行B線程
注意:join時(shí)間參數(shù)缺省的情況,默認(rèn)是0,也就是說(shuō)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類里的源碼,可以看出默認(rèn)賦值為0,然后這個(gè)0是什么意思?0不是表示執(zhí)行0s,而是表示要A線程執(zhí)行完成才繼續(xù)執(zhí)行B線程的意思
ok,然后為什么調(diào)用了join方法就可以實(shí)現(xiàn)線程同步?我們簡(jiǎn)單看一下代碼:
public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; //執(zhí)行時(shí)間必須為正數(shù) if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } //執(zhí)行時(shí)間為0或者缺省情況 if (millis == 0) { while (isAlive()) {//表示線程還沒(méi)執(zhí)行好 wait(0);//調(diào)用線程的wait方法 } } else {//執(zhí)行時(shí)間大于0的情況 while (isAlive()) { long delay = millis - now;//循環(huán)計(jì)算延期時(shí)間 if (delay <= 0) { break; } wait(delay);//同樣調(diào)用線程的wait方法 now = System.currentTimeMillis() - base; } } }
ok,看了一下源碼,還是比較容易理解的,其實(shí)就是調(diào)用了現(xiàn)場(chǎng)wait方法實(shí)現(xiàn)線程同步的
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用IDEA向Gitee提交SpringBoot項(xiàng)目進(jìn)行遠(yuǎn)程管理
本文主要介紹了使用IDEA向Gitee提交SpringBoot項(xiàng)目進(jìn)行遠(yuǎn)程管理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01idea打開項(xiàng)目后無(wú)法顯示目錄結(jié)構(gòu),只能顯示.iml文件問(wèn)題
這篇文章主要介紹了idea打開項(xiàng)目后無(wú)法顯示目錄結(jié)構(gòu),只能顯示.iml文件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Spring?Lifecycle?和?SmartLifecycle區(qū)別面試精講
這篇文章主要為大家介紹了Spring?Lifecycle和SmartLifecycle的區(qū)別面試精講,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10java設(shè)計(jì)模式之單例模式的詳解及優(yōu)點(diǎn)
這篇文章主要介紹了java設(shè)計(jì)模式之單例模式的詳解及優(yōu)點(diǎn)的相關(guān)資料,如果一個(gè)類始終只能創(chuàng)建一個(gè)實(shí)例,那么這個(gè)類被稱為單例類,這種設(shè)計(jì)模式被稱為單例模式,需要的朋友可以參考下2017-08-08SpringCloud 微服務(wù)最佳開發(fā)實(shí)踐
本文結(jié)合我們實(shí)際的開發(fā)中遇到的一些問(wèn)題整理出了一份微服務(wù)開發(fā)的實(shí)踐規(guī)范,對(duì)SpringCloud 微服務(wù)開發(fā)實(shí)踐相關(guān)知識(shí)感興趣的朋友一起看看吧2021-07-07spring?IOC容器管理必須知道的一些操作(基于XML方式)
Spring框架的核心是Spring容器,容器創(chuàng)建對(duì)象,將它們裝配在一起,配置它們并管理它們的完整生命周期,下面這篇文章主要給大家介紹了關(guān)于spring?IOC容器管理必須知道的一些操作,需要的朋友可以參考下2022-03-03