一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - JAVA多線程之方法 JOIN詳解及實例代碼

JAVA多線程之方法 JOIN詳解及實例代碼

2020-08-11 19:05java教程網 Java教程

這篇文章主要介紹了JAVA多線程之方法 JOIN詳解及實例代碼的相關資料,需要的朋友可以參考下

JAVA多線程 JOIN

 對于Java開發人員,多線程應該是必須熟練應用的知識點,特別是開發基于Java語言的產品。本文將深入淺出的表述Java多線程的知識點,在后續的系列里將側重于Java5由Doug Lea教授提供的Concurrent并行包的設計思想以及具體實現與應用。

    如何才能深入淺出呢,我的理解是帶著問題,而不是泛泛的看。所以該系列基本以解決問題為主,當然我也非常希望讀者能夠提出更好的解決問題的方案以及提出更多的問題。由于水平有限,如果有什么錯誤之處,請大家提出,共同討論,總之,我希望通過該系列我們能夠深入理解Java多線程來解決我們實際開發的問題。

    作為開發人員,我想沒有必要討論多線程的基礎知識,比如什么是線程? 如何創建等 ,這些知識點是可以通過書本和Google獲得的。本系列主要是如何理深入解多線程來幫助我們平時的開發,比如線程池如何實現? 如何應用鎖等。 

(1)方法Join是干啥用的? 簡單回答,同步,如何同步? 怎么實現的? 下面將逐個回答。
    自從接觸Java多線程,一直對Join理解不了。JDK是這樣說的:

?
1
2
3
join
public final void join(long millis)throws InterruptedException
Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.

 大家能理解嗎? 字面意思是等待一段時間直到這個線程死亡,我的疑問是那個線程,是它本身的線程還是調用它的線程的,上代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package concurrentstudy;
/**
 *
 * @author vma
 */
public class JoinTest {
  public static void main(String[] args) {
    Thread t = new Thread(new RunnableImpl());
    t.start();
    try {
      t.join(1000);
      System.out.println("joinFinish");
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   
    }
  }
}
class RunnableImpl implements Runnable {
 
  @Override
  public void run() {
    try {
      System.out.println("Begin sleep");
      Thread.sleep(1000);
      System.out.println("End sleep");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
 
  }
}

結果是:

?
1
2
3
Begin sleep
End sleep
joinFinish

明白了吧,當main線程調用t.join時,main線程等待t線程,等待時間是1000,如果t線程Sleep 2000呢 

?
1
2
3
4
5
6
7
8
9
10
11
public void run() {
   try {
     System.out.println("Begin sleep");
     // Thread.sleep(1000);
     Thread.sleep(2000);
     System.out.println("End sleep");
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
 
 }

結果是:

?
1
2
3
Begin sleep
joinFinish
End sleep

也就是說main線程只等1000毫秒,不管T什么時候結束,如果是t.join()呢, 看代碼:  

?
1
2
3
public final void join() throws InterruptedException {
  join(0);
  }

就是說如果是t.join() = t.join(0) 0 JDK這樣說的 A timeout of 0 means to wait forever 字面意思是永遠等待,是這樣嗎?
其實是等到t結束后。

這個是怎么實現的嗎? 看JDK代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
  * Waits at most <code>millis</code> milliseconds for this thread to
  * die. A timeout of <code>0</code> means to wait forever.
  *
  * @param   millis  the time to wait in milliseconds.
  * @exception 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);
   }
 } else {
   while (isAlive()) {
   long delay = millis - now;
   if (delay <= 0) {
     break;
   }
   wait(delay);
   now = System.currentTimeMillis() - base;
   }
 }
 }

其實Join方法實現是通過wait(小提示:Object 提供的方法)。 當main線程調用t.join時候,main線程會獲得線程對象t的鎖(wait 意味著拿到該對象的鎖),調用該對象的wait(等待時間),直到該對象喚醒main線程,比如退出后。

這就意味著main 線程調用t.join時,必須能夠拿到線程t對象的鎖,如果拿不到它是無法wait的,剛開的例子t.join(1000)不是說明了main線程等待1秒,如果在它等待之前,其他線程獲取了t對象的鎖,它等待時間可不就是1毫秒了。上代碼介紹:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package concurrentstudy;
/**
 *
 * @author vma
 */
public class JoinTest {
  public static void main(String[] args) {
    Thread t = new Thread(new RunnableImpl());
    new ThreadTest(t).start();
    t.start();
    try {
      t.join();
      System.out.println("joinFinish");
    } catch (InterruptedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
   
    }
  }
}
class ThreadTest extends Thread {
 
  Thread thread;
 
  public ThreadTest(Thread thread) {
    this.thread = thread;
  }
 
  @Override
  public void run() {
    holdThreadLock();
  }
 
  public void holdThreadLock() {
    synchronized (thread) {
      System.out.println("getObjectLock");
      try {
        Thread.sleep(9000);
 
      } catch (InterruptedException ex) {
       ex.printStackTrace();
      }
      System.out.println("ReleaseObjectLock");
    }
 
  }
}
 
class RunnableImpl implements Runnable {
 
  @Override
  public void run() {
    try {
      System.out.println("Begin sleep");
      Thread.sleep(2000);
      System.out.println("End sleep");
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
 
 
  }
}

在main方法中 通過new ThreadTest(t).start();實例化ThreadTest 線程對象, 它在holdThreadLock()方法中,通過synchronized (thread),獲取線程對象t的鎖,并Sleep(9000)后釋放,這就意味著,即使main方法t.join(1000),等待一秒鐘,它必須等待ThreadTest 線程釋放t鎖后才能進入wait方法中,它實際等待時間是9000+1000 MS

運行結果是:

?
1
2
3
4
5
getObjectLock
Begin sleep
End sleep
ReleaseObjectLock
joinFinish

小結:

本節主要深入淺出join及JDK中的實現。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久无码人妻AV精品一区 | 欧洲老妇人70 | 亚洲福利区 | 国产免费小视频 | 久久亚洲一级α片 | 国产人成激情视频在线观看 | 亚洲色导航 | 国产成人精品在线 | 亚洲AV精品一区二区三区不卡 | 色涩导航 | 丝袜兔女郎被啪在线观看91 | 色哟哟在线观看 | 精品免费视频 | 免费国产高清精品一区在线 | 福利视频导航大全 | 国产目拍亚洲精品一区二区三区 | 欧美日韩中文国产一区二区三区 | 波多野结衣中文字幕 | 亚洲一区二区日韩欧美gif | 国产免费美女视频 | 国产精品自在欧美一区 | 国产在线精品亚洲第一区香蕉 | 女人把扒开给男人爽 | 1024人成网站色 | 天天做天天爱天天综合网 | 无码人妻少妇色欲AV一区二区 | www黄| 亚洲欧美日韩精品高清 | 香蕉久久一区二区三区 | 日本一区二区免费在线观看 | 久久亚洲精品专区蓝色区 | 97影院秋霞国产精品 | 色99视频 | 洗濯屋动漫在线观看 | 9420高清完整版在线观看国语 | 久久婷婷五月免费综合色啪 | 国产一区二区三区久久精品小说 | 91tv破解版不限次数 | 牧教师| 日韩精品免费一区二区三区 | 久久黄色小视频 |