最近在编程遇到了个需要异步执行的操作,经过了一番折腾,发现在主子线程操作中join()方法是非常实用且有效的一个方法.
先来看join()及其重载(overload)方法的说明和代码:
join()方法:
join(long millis)方法:
join(long millis, int nanos)方法:
在使用中需要注意的地方有:
1. join()方法定义在Thread类中,所以只有线程可以直接调用它.
2. join()方法会有可能抛出InterruptedException,需要用try/catch语句块包围;
3. join()方法的作用是"Waits for this thread to die",所以你要start()该线程先.
下面再看一个使用join()方法的实例:
输出结果为:
通过这个例子可以更好地理解"Waits for this thread to die"的含义.
先来看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 }
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 }
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
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 }
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 !
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"的含义.