程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java Thread類的join()方法小結

Java Thread類的join()方法小結

編輯:關於JAVA

最近在編程遇到了個需要異步執行的操作,經過了一番折騰,發現在主子線程 操作中join()方法是非常實用且有效的一個方法.

先來看join()及其重載(overload)方法的說明和代碼:

join()方法:

 1     /**
  2      * Waits for this thread to die.
  3      *
  4      * @exception  InterruptedException if another  thread has interrupted
  5      *             the current thread.   The <i>interrupted status</i> of the
  6      *             current thread is  cleared when this exception is thrown.
  7      */
  8     public final void join() throws  InterruptedException {
  9         join(0);
10     }

join(long millis)方法:

1     /**
  2      * Waits at most <code>millis</code>  milliseconds for this thread to
  3      * die. A timeout of <code>0</code>  means to wait forever.
  4      *
  5      * @param      millis   the time to wait  in milliseconds.
  6      * @exception  InterruptedException if another  thread has interrupted
  7      *             the current thread.   The <i>interrupted status</i> of the
  8      *             current thread is  cleared when this exception is thrown.
  9      */
10     public final synchronized void join(long millis) throws InterruptedException {
11
12         long base = System.currentTimeMillis();
13         long now = 0;
14
15         if (millis < 0) {
16             throw new IllegalArgumentException ("timeout value is negative");
17         }
18
19         if (millis == 0) {
20             while (isAlive()) {
21                 wait(0);
22             }
23         } else {
24             while (isAlive()) {
25                 long delay = millis -  now;
26                 if (delay <= 0) {
27                     break;
28                 }
29                 wait(delay);
30                 now = System.currentTimeMillis () - base;
31             }
32         }
33     }

join(long millis, int nanos)方法:

1     /**
  2      * Waits at most <code>millis</code>  milliseconds plus
  3      * <code>nanos</code> nanoseconds for  this thread to die.
  4      *
  5      * @param      millis   the time to wait  in milliseconds.
  6      * @param      nanos    0-999999  additional nanoseconds to wait.
  7      * @exception  IllegalArgumentException  if the  value of millis is negative
  8      *               the value of nanos  is not in the range 0-999999.
  9      * @exception  InterruptedException if another  thread has interrupted
10      *             the current thread.   The <i>interrupted status</i> of the
11      *             current thread is  cleared when this exception is thrown.
12      */
13     public final synchronized void join(long millis,  int nanos) throws InterruptedException {
15
16         if (millis < 0) {
17             throw new IllegalArgumentException ("timeout value is negative");
18         }
19
20         if (nanos < 0 || nanos > 999999)  {
21             throw new IllegalArgumentException ("nanosecond timeout value out of range");
23         }
24
25         if (nanos >= 500000 || (nanos != 0  && millis == 0)) {
26             millis++;
27         }
28
29         join(millis);
30     }
31

在使用中需要注意的地方有:

1. join()方法定義在Thread類中,所以只有線程可以直接調用它.

2. join()方法會有可能拋出InterruptedException,需要用try/catch語句塊 包圍;

3. join()方法的作用是"Waits for this thread to die",所以你要start() 該線程先.

下面再看一個使用join()方法的實例:

1 import java.io.*;
  2
  3 public class JoinDemo {
  4
  5         public static void main(String[] args) {
  6
  7               ThreadMain main = new  ThreadMain();
  8               //啟動主線程
  9               main.start();
10
11       }
12 }
13
14 //主線程類
15 class ThreadMain extends Thread{
16
17         ThreadSub sub = new ThreadSub();
18
19         public void run(){
20
21               System.out.println("ThreadMain  starts!");
22               //啟動子線程
23               sub.start();
24
25               System.out.println("ThreadMain  running before threadsub!");
26
27               try{
28                   sub.join();
29               }catch(InterruptedException e){
30                 e.printStackTrace();
31               }
32
33               System.out.println("ThreadMain  running after threadsub!");
34
35               System.out.println("ThreadMain  ends!");
36       }
37 }
38
39 //子線程類
40 class ThreadSub extends Thread{
41
42         public void run(){
43
44                 System.out.println("ThreadSub  starts!");
45
46                 for(int i = 0; i < 10;  i++){
47                    System.out.println ("ThreadSub running: " + (i+1));
48                 }
49
50                 System.out.println("ThreadSub  ends!");
51       }
52 }

輸出結果為:

ThreadMain starts!
ThreadMain running before threadsub!
ThreadSub starts!
ThreadSub running: 1
ThreadSub running: 2
ThreadSub running: 3
ThreadSub running: 4
ThreadSub running: 5
ThreadSub running: 6
ThreadSub running: 7
ThreadSub running: 8
ThreadSub running: 9
ThreadSub running: 10
ThreadSub ends!
ThreadMain running after threadsub!
ThreadMain ends!

通過這個例子可以更好地理解"Waits for this thread to die"的含義.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved