JAVA多線程中join()方法的使用方法
雖然關于討論線程join()方法的博客已經非常極其特別多了,但是前幾天我有一個困惑卻沒有能夠得到詳細解釋,就是當系統中正在運行多個線程時,join()到底是暫停了哪些線程,大部分博客給的例子看起來都像是t.join()方法會使所有線程都暫停并等待t的執行完畢。當然,這也是因為我對多線程中的各種方法和同步的概念都理解的不是很透徹。通過看別人的分析和自己的實踐之后終于想明白了,詳細解釋一下希望能幫助到和我有相同困惑的同學。
首先給出結論:t.join()方法只會使主線程(或者說調用t.join()的線程)進入等待池并等待t線程執行完畢后才會被喚醒。并不影響同一時刻處在運行狀態的其他線程。
下面則是分析過程。
之前對于join()方法只是了解它能夠使得t.join()中的t優先執行,當t執行完后才會執行其他線程。能夠使得線程之間的并行執行變成串行執行。
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); } }}
運行結果:
A-1B-1B-2B-3A-2B-4A-3B-5A-4A-5
可以看出A線程和B線程是交替執行的。
而在其中加入join()方法后(后面的代碼都略去了ThreadTest類的定義)
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(); }}
運行結果:
A-1A-2A-3A-4A-5B-1B-2B-3B-4B-5
顯然,使用t1.join()之后,B線程需要等A線程執行完畢之后才能執行。需要注意的是,t1.join()需要等t1.start()執行之后執行才有效果,此外,如果t1.join()放在t2.start()之后的話,仍然會是交替執行,然而并不是沒有效果,這點困擾了我很久,也沒在別的博客里看到過。
為了深入理解,我們先看一下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無限時間直到被notify }} else { while (isAlive()) {long delay = millis - now;if (delay <= 0) { break;}wait(delay);now = System.currentTimeMillis() - base; }} }
可以看出,join()方法的底層是利用wait()方法實現的。可以看出,join方法是一個同步方法,當主線程調用t1.join()方法時,主線程先獲得了t1對象的鎖,隨后進入方法,調用了t1對象的wait()方法,使主線程進入了t1對象的等待池,此時,A線程則還在執行,并且隨后的t2.start()還沒被執行,因此,B線程也還沒開始。等到A線程執行完畢之后,主線程繼續執行,走到了t2.start(),B線程才會開始執行。
此外,對于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('t2start'); t2.start(); System.out.println('t3start'); t3.start(); System.out.println(Thread.currentThread().getName()+' end'); } }
運行結果為
main startt1startt1endt2startt2endt3startt3endA-1A-2main endC-1C-2C-3C-4C-5A-3B-1B-2B-3B-4B-5A-4A-5
A、B、C和主線程交替運行。加入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'); } }
運行結果:
main startt1startt1endt2startt2endA-1B-1A-2A-3A-4A-5B-2t3startt3endB-3main endB-4B-5C-1C-2C-3C-4C-5
多次實驗可以看出,主線程在t1.join()方法處停止,并需要等待A線程執行完畢后才會執行t3.start(),然而,并不影響B線程的執行。因此,可以得出結論,t.join()方法只會使主線程進入等待池并等待t線程執行完畢后才會被喚醒。并不影響同一時刻處在運行狀態的其他線程。
PS:join源碼中,只會調用wait方法,并沒有在結束時調用notify,這是因為線程在die的時候會自動調用自身的notifyAll方法,來釋放所有的資源和鎖。
到此這篇關于JAVA多線程中join()方法的使用方法的文章就介紹到這了,更多相關JAVA多線程join()方法內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
