java中join方法的作用是什么-創(chuàng)新互聯(lián)

java中join方法的作用是什么?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

目前創(chuàng)新互聯(lián)已為成百上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站托管維護、企業(yè)網(wǎng)站設(shè)計、湞江網(wǎng)站維護等服務(wù),公司將堅持客戶導向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

前言:

java 中的 join() 方法在多線程中會涉及到,這個方法最初理解起來可能有點抽象,用一兩次大概就懂了。簡單說就是當前線程等待調(diào)用join方法的線程結(jié)束才能繼續(xù)往下執(zhí)行。

1. 舉個例子

如下,

MyRunnable 類是實現(xiàn) Runnable 接口的多線程類,其run() 方法是一個計算,計算值存儲在 result 字段,獲取計算結(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());
 }
}

獲取計算結(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方法以后當前線程(在這里就是main函數(shù))會等待thread_1 結(jié)束后才繼續(xù)執(zhí)行下面的代碼。

2. jion() 方法源碼解析

其實 join() 方法內(nèi)部的實現(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ù)會默認millis 為 0
   while (isAlive()) {
    wait(0);
   }
  } else {
   while (isAlive()) {
    long delay = millis - now;
    if (delay <= 0) {
     break;
    }
    wait(delay);
    now = System.currentTimeMillis() - base;
   }
  }
 }

當然上述還涉及 Object 類的 wait() 方法,感興趣可以查一下,這里可以簡單的理解就是一個等待多少時間。

關(guān)于java中join方法的作用是什么問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

本文標題:java中join方法的作用是什么-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article14/dddege.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)頁設(shè)計公司網(wǎng)站維護、網(wǎng)站內(nèi)鏈、網(wǎng)站設(shè)計公司ChatGPT、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站建設(shè)