t.join

The join method allows one thread wait until the completion of another.

If t1 is a Thread  object whose thread(t) is currently excuting,

t1.join()

cause the current thread t to pause execution util t1 terminates.

package thread;

public class CustomThread1 extends Thread { 
	public CustomThread1(){
		super("[CustomThread1] Thread");
	}

	@Override
	public void run() {
		String threadName = Thread.currentThread().getName();
		System.out.println(threadName +" start");
		try {
			for(int i=0; i<5; i++){
				System.out.println(threadName +" loop at"+i);
				Thread.sleep(1000);
			}
			System.out.println(threadName + " end");
		} catch (InterruptedException e) {
			System.out.println("Exception from "+threadName+".run");
		}
	}
	
}package thread;

public class CustomThread extends Thread {
	public static void main(String[] args){
		String threadName = Thread.currentThread().getName();
		System.out.println(threadName + " start.");
		CustomThread1 t1 = new CustomThread1();
		CustomThread t = new CustomThread(t1);
		
		try {
			t1.start();
			Thread.sleep(2000);//让Main 睡2秒,使得其他thread有机会执行
			t.start();
			t.join();//运行结果2将此处注释掉
			
		} catch (InterruptedException e) {
			System.out.println("Exception from main");
		}
		System.out.println(threadName + " end!");
		
	}
	
	CustomThread1 t1;
	public CustomThread(CustomThread1 t1){
		super("[CustomThread] Thread");
		this.t1 = t1;
	}
	
	@Override
	public void run(){
		String threadName = Thread.currentThread().getName();
		System.out.println(threadName + " start ");
		// If t is a Thread object whose thread is currently executing,
		//t.join()causes the current thread to pause execution until t's thread terminates
		try {
			t1.join();
			System.out.println(threadName+" end.");
		} catch (InterruptedException e) {
			System.out.println("Exception from "+threadName+".run");
		}
	}
	
	

}

1.加入t.join()运行结果

main start.//main方法所在的线程起动,但没有马上结束,因为调用t.join();,所以要等到t结束了,此线程才能向下执行。

[CustomThread1] Thread start.//线程CustomThread1起动

[CustomThread1] Thread loop at 0//线程CustomThread1执行

[CustomThread1] Thread loop at 1//线程CustomThread1执行

[CustomThread] Thread start.//线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。

[CustomThread1] Thread loop at 2//线程CustomThread1继续执行

[CustomThread1] Thread loop at 3//线程CustomThread1继续执行

[CustomThread1] Thread loop at 4//线程CustomThread1继续执行

[CustomThread1] Thread end. //线程CustomThread1结束了

[CustomThread] Thread end.// 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果

main end!//线程CustomThread结束,此线程在t.join();阻塞处起动,向下继续执行的结果。


2.注释掉t.join()运行结果

main start. // main方法所在的线程起动,但没有马上结束,这里并不是因为join方法,而是因为Thread.sleep(2000);

[CustomThread1] Thread start. //线程CustomThread1起动

[CustomThread1] Thread loop at 0//线程CustomThread1执行

[CustomThread1] Thread loop at 1//线程CustomThread1执行

main end!// Thread.sleep(2000);结束,虽然在线程CustomThread执行了t1.join();,但这并不会影响到其他线程(这里main方法所在的线程)。

[CustomThread] Thread start. //线程CustomThread起动,但没有马上结束,因为调用t1.join();,所以要等到t1结束了,此线程才能向下执行。

[CustomThread1] Thread loop at 2//线程CustomThread1继续执行

[CustomThread1] Thread loop at 3//线程CustomThread1继续执行

[CustomThread1] Thread loop at 4//线程CustomThread1继续执行

[CustomThread1] Thread end. //线程CustomThread1结束了

[CustomThread] Thread end. // 线程CustomThread在t1.join();阻塞处起动,向下继续执行的结果

三、从源码看join()方法

 

在CustomThread的run方法里,执行了t1.join();,进入看一下它的JDK源码:

 

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

然后进入join(0)方法:

 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)  // 参数millis为0.   
11     throws  InterruptedException  {   
12long base = System.currentTimeMillis();   
13long now = 0;   
14if (millis < 0{   
15           throw new IllegalArgumentException("timeout value is negative");   
16}
   
17if (millis == 0{//进入这个分支   
18    while (isAlive()) {//判断本线程是否为活动的。这里的本线程就是t1.   
19    wait(0);//阻塞   
20    }
   
21}
 else {   
22    while (isAlive()) {   
23    long delay = millis - now;   
24    if (delay <= 0{   
25        break;   
26    }
   
27    wait(delay);   
28    now = System.currentTimeMillis() - base;   
29    }
   
30}
   
31   }
 

 

单纯从代码上看,如果线程被生成了,但还未被起动,调用它的join()方法是没有作用的。将直接继续向下执行,这里就不写代码验证了。






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值