欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JAVA多線程中join()方法的使用方法

 更新時(shí)間:2021年05月18日 15:59:43   作者:月色MOON  
雖然關(guān)于討論線程join()方法的博客已經(jīng)非常極其特別多了,但是前幾天我有一個(gè)困惑卻沒(méi)有能夠得到詳細(xì)解釋?zhuān)?dāng)系統(tǒng)中正在運(yùn)行多個(gè)線程時(shí),join()到底是暫停了哪些線程,所以本文詳細(xì)解釋一下希望能幫助到和我有相同困惑的同學(xué)

雖然關(guān)于討論線程join()方法的博客已經(jīng)非常極其特別多了,但是前幾天我有一個(gè)困惑卻沒(méi)有能夠得到詳細(xì)解釋?zhuān)褪钱?dāng)系統(tǒng)中正在運(yùn)行多個(gè)線程時(shí),join()到底是暫停了哪些線程,大部分博客給的例子看起來(lái)都像是t.join()方法會(huì)使所有線程都暫停并等待t的執(zhí)行完畢。當(dāng)然,這也是因?yàn)槲覍?duì)多線程中的各種方法和同步的概念都理解的不是很透徹。通過(guò)看別人的分析和自己的實(shí)踐之后終于想明白了,詳細(xì)解釋一下希望能幫助到和我有相同困惑的同學(xué)。

首先給出結(jié)論:t.join()方法只會(huì)使主線程(或者說(shuō)調(diào)用t.join()的線程)進(jìn)入等待池并等待t線程執(zhí)行完畢后才會(huì)被喚醒。并不影響同一時(shí)刻處在運(yùn)行狀態(tài)的其他線程。

下面則是分析過(guò)程。

之前對(duì)于join()方法只是了解它能夠使得t.join()中的t優(yōu)先執(zhí)行,當(dāng)t執(zhí)行完后才會(huì)執(zhí)行其他線程。能夠使得線程之間的并行執(zhí)行變成串行執(zhí)行。

package CSDN;
public class TestJoin {
 
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub
  ThreadTest t1=new ThreadTest("A");
  ThreadTest t2=new ThreadTest("B");
  t1.start();
  t2.start();
 }
 
 
}
class ThreadTest extends Thread {
 private String name;
 public ThreadTest(String name){
  this.name=name;
 }
 public void run(){
  for(int i=1;i<=5;i++){
    System.out.println(name+"-"+i);
  }  
 }
}

運(yùn)行結(jié)果:

A-1
B-1
B-2
B-3
A-2
B-4
A-3
B-5
A-4
A-5

可以看出A線程和B線程是交替執(zhí)行的。

而在其中加入join()方法后(后面的代碼都略去了ThreadTest類(lèi)的定義)

package CSDN;
public class TestJoin {
 
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub
  ThreadTest t1=new ThreadTest("A");
  ThreadTest t2=new ThreadTest("B");
  t1.start();
  t1.join();
  t2.start();
 }
}

運(yùn)行結(jié)果:

A-1
A-2
A-3
A-4
A-5
B-1
B-2
B-3
B-4
B-5

顯然,使用t1.join()之后,B線程需要等A線程執(zhí)行完畢之后才能執(zhí)行。需要注意的是,t1.join()需要等t1.start()執(zhí)行之后執(zhí)行才有效果,此外,如果t1.join()放在t2.start()之后的話,仍然會(huì)是交替執(zhí)行,然而并不是沒(méi)有效果,這點(diǎn)困擾了我很久,也沒(méi)在別的博客里看到過(guò)。

為了深入理解,我們先看一下join()的源碼。

    /**
     * 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);            //join()等同于join(0)
    }
    /**
     * Waits at most {@code millis} milliseconds for this thread to
     * die. A timeout of {@code 0} means to wait forever.
     *
     * <p> This implementation uses a loop of {@code this.wait} calls
     * conditioned on {@code this.isAlive}. As a thread terminates the
     * {@code this.notifyAll} method is invoked. It is recommended that
     * applications not use {@code wait}, {@code notify}, or
     * {@code notifyAll} on {@code Thread} instances.
     *
     * @param  millis
     *         the time to wait in milliseconds
     *
     * @throws  IllegalArgumentException
     *          if the value of {@code millis} is negative
     *
     * @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 synchronized void join(long millis) throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;
 
        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }
 
        if (millis == 0) {
            while (isAlive()) {
                wait(0);           //join(0)等同于wait(0),即wait無(wú)限時(shí)間直到被notify
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

可以看出,join()方法的底層是利用wait()方法實(shí)現(xiàn)的??梢钥闯觯琷oin方法是一個(gè)同步方法,當(dāng)主線程調(diào)用t1.join()方法時(shí),主線程先獲得了t1對(duì)象的鎖,隨后進(jìn)入方法,調(diào)用了t1對(duì)象的wait()方法,使主線程進(jìn)入了t1對(duì)象的等待池,此時(shí),A線程則還在執(zhí)行,并且隨后的t2.start()還沒(méi)被執(zhí)行,因此,B線程也還沒(méi)開(kāi)始。等到A線程執(zhí)行完畢之后,主線程繼續(xù)執(zhí)行,走到了t2.start(),B線程才會(huì)開(kāi)始執(zhí)行。

此外,對(duì)于join()的位置和作用的關(guān)系,我們可以用下面的例子來(lái)分析

package CSDN;
 
public class TestJoin {
 
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub
  System.out.println(Thread.currentThread().getName()+" start");
  ThreadTest t1=new ThreadTest("A");
  ThreadTest t2=new ThreadTest("B");
  ThreadTest t3=new ThreadTest("C");
  System.out.println("t1start");
  t1.start();
  System.out.println("t2start");
  t2.start();
  System.out.println("t3start");
  t3.start();
  System.out.println(Thread.currentThread().getName()+" end");
 } 
}

運(yùn)行結(jié)果為

main start
t1start
t1end
t2start
t2end
t3start
t3end
A-1
A-2
main end
C-1
C-2
C-3
C-4
C-5
A-3
B-1
B-2
B-3
B-4
B-5
A-4
A-5

A、B、C和主線程交替運(yùn)行。加入join()方法后

package CSDN;
 
public class TestJoin {
 
 public static void main(String[] args) throws InterruptedException {
  // TODO Auto-generated method stub
  System.out.println(Thread.currentThread().getName()+" start");
  ThreadTest t1=new ThreadTest("A");
  ThreadTest t2=new ThreadTest("B");
  ThreadTest t3=new ThreadTest("C");
  System.out.println("t1start");
  t1.start();
  System.out.println("t1end");
  System.out.println("t2start");
  t2.start();
  System.out.println("t2end");
  t1.join();
  System.out.println("t3start");
  t3.start();
  System.out.println("t3end");
  System.out.println(Thread.currentThread().getName()+" end");
 } 
}

運(yùn)行結(jié)果:

main start
t1start
t1end
t2start
t2end
A-1
B-1
A-2
A-3
A-4
A-5
B-2
t3start
t3end
B-3
main end
B-4
B-5
C-1
C-2
C-3
C-4
C-5

多次實(shí)驗(yàn)可以看出,主線程在t1.join()方法處停止,并需要等待A線程執(zhí)行完畢后才會(huì)執(zhí)行t3.start(),然而,并不影響B(tài)線程的執(zhí)行。因此,可以得出結(jié)論,t.join()方法只會(huì)使主線程進(jìn)入等待池并等待t線程執(zhí)行完畢后才會(huì)被喚醒。并不影響同一時(shí)刻處在運(yùn)行狀態(tài)的其他線程。

PS:join源碼中,只會(huì)調(diào)用wait方法,并沒(méi)有在結(jié)束時(shí)調(diào)用notify,這是因?yàn)榫€程在die的時(shí)候會(huì)自動(dòng)調(diào)用自身的notifyAll方法,來(lái)釋放所有的資源和鎖。

到此這篇關(guān)于JAVA多線程中join()方法的使用方法的文章就介紹到這了,更多相關(guān)JAVA多線程join()方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Spring中ApplicationEvent事件機(jī)制源碼詳解

    Spring中ApplicationEvent事件機(jī)制源碼詳解

    這篇文章主要介紹了Spring中ApplicationEvent事件機(jī)制源碼詳解,Spring中與事件有關(guān)的接口和類(lèi)主要包括ApplicationEvent、ApplicationListener,下面來(lái)看一下Spring中事件的具體應(yīng)用,需要的朋友可以參考下
    2023-09-09
  • JDK動(dòng)態(tài)代理過(guò)程原理及手寫(xiě)實(shí)現(xiàn)詳解

    JDK動(dòng)態(tài)代理過(guò)程原理及手寫(xiě)實(shí)現(xiàn)詳解

    這篇文章主要為大家介紹了JDK動(dòng)態(tài)代理過(guò)程原理及手寫(xiě)實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • springmvc整合ssm配置的詳細(xì)代碼

    springmvc整合ssm配置的詳細(xì)代碼

    今天通過(guò)實(shí)例代碼給大家介紹了springmvc整合ssm配置的詳細(xì)方法,代碼簡(jiǎn)單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • 基于java Servlet編碼/異常處理(詳解)

    基于java Servlet編碼/異常處理(詳解)

    下面小編就為大家?guī)?lái)一篇基于java Servlet編碼/異常處理(詳解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • springboot應(yīng)用服務(wù)啟動(dòng)事件的監(jiān)聽(tīng)實(shí)現(xiàn)

    springboot應(yīng)用服務(wù)啟動(dòng)事件的監(jiān)聽(tīng)實(shí)現(xiàn)

    本文主要介紹了springboot應(yīng)用服務(wù)啟動(dòng)事件的監(jiān)聽(tīng)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 詳解SpringBoot容器的生命周期

    詳解SpringBoot容器的生命周期

    在使用SpringBoot進(jìn)行開(kāi)發(fā)時(shí),我們經(jīng)常需要對(duì)Spring容器的生命周期進(jìn)行了解和掌握,本文將介紹SpringBoot容器的生命周期,包括容器的創(chuàng)建、初始化、銷(xiāo)毀等過(guò)程,并提供相應(yīng)的代碼示例
    2023-06-06
  • SpringBoot中集成企業(yè)微信機(jī)器人實(shí)現(xiàn)運(yùn)維報(bào)警的示例

    SpringBoot中集成企業(yè)微信機(jī)器人實(shí)現(xiàn)運(yùn)維報(bào)警的示例

    本文主要介紹了SpringBoot中集成企業(yè)微信機(jī)器人實(shí)現(xiàn)運(yùn)維報(bào)警,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • 一篇文章讓你弄懂Java運(yùn)算符

    一篇文章讓你弄懂Java運(yùn)算符

    java中位運(yùn)算符主要有按位與&、按位或|、按位非~、按位異或^, 在使用時(shí)需要將運(yùn)算數(shù)都轉(zhuǎn)換為二進(jìn)制再進(jìn)行運(yùn)算,若為負(fù)數(shù)則使用補(bǔ)碼表示,這篇文章主要給大家介紹了關(guān)于Java運(yùn)算符的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • java使用pdfbox操作pdf文件示例

    java使用pdfbox操作pdf文件示例

    有時(shí)候PDF中的文字無(wú)法復(fù)制,這可能是因?yàn)镻DF文件加密了,不過(guò)使用PDFBox開(kāi)源軟件就可以把它讀出來(lái),下面是使用示例
    2014-03-03
  • Java 進(jìn)制轉(zhuǎn)換的方法

    Java 進(jìn)制轉(zhuǎn)換的方法

    這篇文章介紹了Java 進(jìn)制轉(zhuǎn)換的方法,有需要的朋友可以參考一下
    2013-09-09

最新評(píng)論