java中join方法的理解與說(shuō)明詳解
前言:
java 中的 join() 方法在多線程中會(huì)涉及到,這個(gè)方法最初理解起來(lái)可能有點(diǎn)抽象,用一兩次大概就懂了。簡(jiǎn)單說(shuō)就是當(dāng)前線程等待調(diào)用join方法的線程結(jié)束才能繼續(xù)往下執(zhí)行。
1. 舉個(gè)例子
如下,
MyRunnable 類是實(shí)現(xiàn) Runnable 接口的多線程類,其run() 方法是一個(gè)計(jì)算,計(jì)算值存儲(chǔ)在 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() 方法來(lái)判斷線程是否結(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() 方法,感興趣可以查一下,這里可以簡(jiǎn)單的理解就是一個(gè)等待多少時(shí)間。
總結(jié)
到此這篇關(guān)于java中join方法的理解與說(shuō)明的文章就介紹到這了,更多相關(guān)java中join方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring?Boot中@Import三種使用方式實(shí)例詳解
這篇文章主要介紹了Spring?Boot中@Import三種使用方式,主要有引入普通類,引入importSelector的實(shí)現(xiàn)類及引入importBeanDefinitionRegister的實(shí)現(xiàn)類,結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2022-11-11web 容器的設(shè)計(jì)如何實(shí)現(xiàn)
這篇文章主要介紹了web 容器的設(shè)計(jì)如何實(shí)現(xiàn)的相關(guān)資料,本文旨在介紹如何設(shè)計(jì)一個(gè)web容器,只探討實(shí)現(xiàn)的思路,并不涉及過(guò)多的具體實(shí)現(xiàn)。把它分解劃分成若干模塊和組件,每個(gè)組件模塊負(fù)責(zé)不同的功能,需要的朋友可以參考下2016-12-12IDEA2020.1個(gè)性化設(shè)置的實(shí)現(xiàn)
這篇文章主要介紹了IDEA2020.1個(gè)性化設(shè)置的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Netty分布式flush方法刷新buffer隊(duì)列源碼剖析
這篇文章主要為大家介紹了Netty分布式flush方法刷新buffer隊(duì)列源碼剖析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03