Thread.join()方法详解

API:

 

join

public final void join()
                throws InterruptedException
等待该线程终止。
抛出:
InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的 中断状态 被清除。

 

 

join

public final void join(long millis)
                throws InterruptedException
等待该线程终止的时间最长为  millis 毫秒。超时为  0 意味着要一直等下去。
参数:
millis - 以毫秒为单位的等待时间。
抛出:
InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的 中断状态 被清除。

 

 

join

public final void join(long millis,
                       int nanos)
                throws InterruptedException
等待该线程终止的时间最长为  millis 毫秒 +  nanos 纳秒。
参数:
millis - 以毫秒为单位的等待时间。
nanos - 要等待的 0-999999 附加纳秒。
抛出:
IllegalArgumentException - 如果 millis 值为负,则 nanos 的值不在 0-999999 范围内。
InterruptedException - 如果任何线程中断了当前线程。当抛出该异常时,当前线程的 中断状态 被清除。

解析

 

Thread.join(),是用来指定当前主线程等待其他线程执行完毕后,再来继续执行Thread.join()后面的代码

Thread.join(1000s),这句话的意思是,只要满足下面2个条件中的一个时,主线程就会继续执行thread.join(1000s)后面的代码:

条件1:thread 执行完毕;

条件2:已经等待 thread 执行了1000ms.

 

Demo:

package cn.com.common.thread;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * 
* @ClassName: ThreadJoinTest 
* @Description:Thread.join()方法解析
* @author linsky328
* @date 2017年7月4日 上午10:22:36 
*
 */
public class ThreadJoinTest  {
	
	 public static void main(String[] args){  
	        DataSourcesLoader dsLoader = new DataSourcesLoader();  
	        Thread thread1 = new Thread(dsLoader,"DataSourceThread");  
	          
	        NetworkConnectionsLoader ncLoader = new NetworkConnectionsLoader();  
	        Thread thread2 = new Thread(ncLoader,"NetworkConnectionLoader");  
	          
	        thread1.start();  
	        thread2.start();   
	          
	        try { 
	        	/**
	        	 * Thread.join(),是用来指定当前主线程等待其他线程执行完毕后,再来继续执行Thread.join()后面的代码。
	        	 */
	            thread1.join();  
	            /**
	        	 * Thread.join(1900),是用来指定当前主线程等待其他线程执行1.9秒后,再来继续执行Thread.join(1900)后面的代码。
	        	 */
	            thread2.join(1900);  
	          } catch (InterruptedException e) {  
	            e.printStackTrace();  
	          }  
	            
	          System.out.printf("Main: Configuration has been loaded: %s\n",new Date());  
	    }  
	
}


class DataSourcesLoader implements Runnable{  
  
    @Override  
    public void run() {  
        System.out.printf("Beginning data sources loading: %s\n",new Date());  
        try {  
          TimeUnit.SECONDS.sleep(4);  
        } catch (InterruptedException e) {  
          e.printStackTrace();  
        }  
        System.out.printf("Data sources loading has finished: %s\n",new Date());  
    }  
  
}


class NetworkConnectionsLoader implements Runnable{  
	  
    @Override  
    public void run() {  
        System.out.printf("Beginning network connect loading: %s\n",new Date());  
        try {  
          TimeUnit.SECONDS.sleep(6);  
        } catch (InterruptedException e) {  
          e.printStackTrace();  
        }  
        System.out.printf("Network connect loading has finished: %s\n",new Date());  
          
    } 
}  

 运行结果:

Beginning data sources loading: Tue Jul 04 11:19:18 CST 2017

Beginning network connect loading: Tue Jul 04 11:19:18 CST 2017

Data sources loading has finished: Tue Jul 04 11:19:23 CST 2017

Main: Configuration has been loaded: Tue Jul 04 11:19:24 CST 2017

Network connect loading has finished: Tue Jul 04 11:19:25 CST 2017

猜你喜欢

转载自linsky328.iteye.com/blog/2382699