java中join方法的理解與說明詳解
前言:
java 中的 join() 方法在多線程中會(huì)涉及到,這個(gè)方法最初理解起來可能有點(diǎn)抽象,用一兩次大概就懂了。簡單說就是當(dāng)前線程等待調(diào)用join方法的線程結(jié)束才能繼續(xù)往下執(zhí)行。
1. 舉個(gè)例子
如下,
MyRunnable 類是實(shí)現(xiàn) Runnable 接口的多線程類,其run() 方法是一個(gè)計(jì)算,計(jì)算值存儲在 result 字段,獲取計(jì)算結(jié)果就必須等線程執(zhí)行完之后調(diào)用 getResult() 獲取
public class MyRunnable implements Runnable {
private int num;
private String threadName;
private long result;
public MyRunnable(int num, String threadName) {
this.threadName = threadName;
this.num = num;
}
public void run() {
for (int i = 0; i < num; i++) {
result += i;
}
}
public long getResult() {
return result;
}
}
public class NormalTest {
public static void main(String[] args) {
normal();
}
private static void normal() {
MyRunnable myRunnable_1 = new MyRunnable(1000, "runnable_1");
Thread thread_1 = new Thread(myRunnable_1);
thread_1.start();
do {
System.out.println("--------------------------------------------------");
System.out.println("thread status: " + thread_1.isAlive() + ",result: " + myRunnable_1.getResult());
} while (thread_1.isAlive());
}
}
獲取計(jì)算結(jié)果需要持續(xù)判斷線程 thread_1 是否結(jié)束才能最終獲取,輸出如下:
--------------------------------------------------
thread status: true,result: 0
--------------------------------------------------
thread status: true,result: 11026
--------------------------------------------------
thread status: false,result: 499500
而使用join()方法可以省去判斷的麻煩,如下
public class JoinTest {
public static void main(String[] args) {
join();
}
private static void join() {
MyRunnable myRunnable_1 = new MyRunnable(1000, "runnable_1");
Thread thread_1 = new Thread(myRunnable_1);
thread_1.start();
try {
thread_1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("thread status: " + thread_1.isAlive() + ",result: " + myRunnable_1.getResult());
}
}
輸出如下:
thread status: false,result: 499500
調(diào)用join方法以后當(dāng)前線程(在這里就是main函數(shù))會(huì)等待thread_1 結(jié)束后才繼續(xù)執(zhí)行下面的代碼。
2. jion() 方法源碼解析
其實(shí) join() 方法內(nèi)部的實(shí)現(xiàn)跟上面例子中的normal()方法很類似,也是使用線程的 isAlive() 方法來判斷線程是否結(jié)束,核心源碼如下:
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) { // join 方法如果不傳參數(shù)會(huì)默認(rèn)millis 為 0
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
當(dāng)然上述還涉及 Object 類的 wait() 方法,感興趣可以查一下,這里可以簡單的理解就是一個(gè)等待多少時(shí)間。
總結(jié)
到此這篇關(guān)于java中join方法的理解與說明的文章就介紹到這了,更多相關(guān)java中join方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot整合Netty實(shí)現(xiàn)WebSocket的示例代碼
本文主要介紹了SpringBoot整合Netty實(shí)現(xiàn)WebSocket的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
JAVA中字符串函數(shù)subString的用法小結(jié)
本篇文章主要是對JAVA中字符串函數(shù)subString的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
Java如何實(shí)現(xiàn)微信支付v3的支付回調(diào)
這篇文章主要給大家介紹了關(guān)于Java如何實(shí)現(xiàn)微信支付v3的支付回調(diào),微信實(shí)現(xiàn)支付功能與支付寶實(shí)現(xiàn)支付功能是相似的,文中給了詳細(xì)的示例代碼,需要的朋友可以參考下2023-07-07
Spring?MVC中的Controller進(jìn)行單元測試的實(shí)現(xiàn)
本文主要介紹了如何對Spring?MVC中的Controller進(jìn)行單元測試的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
SpringAOP中的切點(diǎn)表達(dá)式Pointcut詳解
這篇文章主要介紹了SpringAOP中的切點(diǎn)表達(dá)式Pointcut詳解,Spring?的?AOP?中的一個(gè)核心概念是切點(diǎn)(Pointcut),切點(diǎn)表達(dá)式定義通知(Advice)執(zhí)行的范圍,需要的朋友可以參考下2023-08-08

