執行緒通訊之join方法

2021-09-25 13:39:32 字數 1019 閱讀 6013

join方法就是掛起呼叫執行緒,直到被呼叫執行緒執行完畢後再繼續執行。例:threadb執行緒中threada的join方法,所以threadb需在threada執行完畢後才繼續執行join後的**,而主線程執行threadb.join(),所以最終主線程需等threada和threadb執行完畢後才繼續。

@slf4j

public class jointhread catch (interruptedexception e)

});thread threadb = new thread(() -> catch (interruptedexception e)

});threada.start();

threadb.start();

threadb.join();

log.info("run time [{}]", starttime - system.currenttimemillis());

log.info("main thread end");

}}

輸出結果:

- threada start

- threada end

- threadb start

- threadb end

- run time [-7149]

- main thread end

thread類中的join方法:

public final synchronized void join(long millis)

throws interruptedexception

//等待超時時間為0則始終掛起,指定被呼叫執行緒執行完畢

if (millis == 0)

} else

//等待指定時間

wait(delay);

now = system.currenttimemillis() - base;}}

}

Java多執行緒 執行緒間通訊之join 方法

1.join 方法 當前執行緒 等待 執行緒物件 銷毀。比如說 j.join 就是當前執行緒 等待 執行緒物件 j 銷毀。package threadcommunication public class jointest extends thread catch interruptedexcepti...

多執行緒Join方法

天意憐幽草,人間重晩晴 a.sleep 5000 讓執行緒睡5秒但是,如果你不知道執行緒b需要執行多長時間,並且在a中需要使用到b中的結果,那麼,這時你就可以使用join方法 下面是具體的例子 可以看到,join long time 方法內部其實是呼叫了wait long time 方法,我們了解到...

多執行緒join 方法

直接 public static void main string args for thread t list int n 0 for thread t list catch interruptedexception e system.out.println 完全結束 static class m...