Thread--Join方法

join()函数是Thread类中定义的一个方法,join()的作用是让主线程等待子线程结束之后才能继续进行,简单的示例说明:

[java]  view plain  copy
  1. // 主线程  
  2. public class Father extends Thread {  
  3.     public void run() {  
  4.         Son s = new Son();  
  5.         s.start();  
  6.         s.join();  
  7.         ...  
  8.     }  
  9. }  
  10. // 子线程  
  11. public class Son extends Thread {  
  12.     public void run() {  
  13.         ...  
  14.     }  
  15. }  

上面有两个类Father类和Son类,并且Son是在Father中创建并启动的,Father是主线程类,Son是子线程类。在Father的主线程中,通过new Son()创建子线程s,接着通过s.start()开启子线程,并调用子线程的s.join()方法,在调用s.join()之后,主线程会一直等到,直到子线程运行完毕。

Thread中join函数源码:
[java]  view plain  copy
  1. public final void join() throws InterruptedException   
  2. {  
  3.         join(0);  
  4. }  
  5. public final synchronized void join(long millis)throws InterruptedException   
  6.  {  
  7.     long base = System.currentTimeMillis();  
  8.     long now = 0;  
  9.   
  10.     if (millis < 0)   
  11.     {  
  12.         throw new IllegalArgumentException("timeout value is negative");  
  13.     }  
  14.   
  15.     if (millis == 0)   
  16.     {  
  17.         while (isAlive())   
  18.          {  
  19.             wait(0);  
  20.          }  
  21.     }else{  
  22.         while (isAlive())   
  23.             {  
  24.                 long delay = millis - now;  
  25.                 if (delay <= 0) {  
  26.                 break;  
  27.                 }  
  28.                 wait(delay);  
  29.                 now = System.currentTimeMillis() - base;  
  30.             }  
  31.     }  
  32. }  
通过源码我们可以了解到,join的主要机制就在于
[java]  view plain  copy
  1. while (isAlive())   
  2.          {  
  3.             wait(0);  
  4.          }  
判断当前子线程是活着的那么一直调用wait(0),使主线程处于阻塞状态。我们之前在线程等待中说到过,wait()的作用是让当前线程等待,当前线程就是指当前在CPU上运行的线程。虽然是调用子线程的wait()方法,但它是通过主线程去调用的,所以休眠的是主线程而不是子线程。

join()方法简单示例:
[java]  view plain  copy
  1. public class JoinTest{  
  2.   
  3.     public static void main(String[] args){  
  4.         try {  
  5.             ThreadA t1 = new ThreadA("t1"); // 新建“线程t1”  
  6.   
  7.             t1.start();                     // 启动“线程t1”  
  8.             t1.join();                        // 将“线程t1”加入到“主线程main”中,并且“主线程main()会等待它的完成”  
  9.             System.out.printf("%s finish\n", Thread.currentThread().getName());  
  10.         } catch (InterruptedException e) {  
  11.             e.printStackTrace();  
  12.         }  
  13.     }  
  14.   
  15.     static class ThreadA extends Thread{  
  16.   
  17.         public ThreadA(String name){  
  18.             super(name);  
  19.         }  
  20.         public void run(){  
  21.             System.out.printf("%s start\n"this.getName());  
  22.   
  23.             // 延时操作  
  24.         for(int i=0; i <1000000; i++);  
  25.   
  26.         System.out.printf("%s finish\n"this.getName());  
  27.        }  
  28.     }  
  29. }  
运行结果:
t1 start
t1 finish
main finish
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值