线程join方法详细探索

虽然关于讨论线程join()方法的博客已经非常极其特别多了,但是前几天我有一个困惑却没有能够得到详细解释,就是当系统中正在运行多个线程时,join()到底是暂停了哪些线程,大部分博客给的例子看起来都像是t.join()方法会使所有线程都暂停并等待t的执行完毕。当然,这也是因为我对多线程中的各种方法和同步的概念都理解的不是很透彻。通过看别人的分析和自己的实践之后终于想明白了,详细解释一下希望能帮助到和我有相同困惑的同学。

首先给出结论:t.join()方法只会使主线程进入等待池并等待t线程执行完毕后才会被唤醒。并不影响同一时刻处在运行状态的其他线程。

下面则是分析过程。

之前对于join()方法只是了解它能够使得t.join()中的t优先执行,当t执行完后才会执行其他线程。能够使得线程之间的并行执行变成串行执行。


   
   
  1. package CSDN;
  2. public class TestJoin {
  3. public static void main(String[] args) throws InterruptedException {
  4. // TODO Auto-generated method stub
  5. ThreadTest t1= new ThreadTest( "A");
  6. ThreadTest t2= new ThreadTest( "B");
  7. t1.start();
  8. t2.start();
  9. }
  10. }
  11. class ThreadTest extends Thread {
  12. private String name;
  13. public ThreadTest(String name){
  14. this.name=name;
  15. }
  16. public void run(){
  17. for( int i= 1;i<= 5;i++){
  18. System.out.println(name+ "-"+i);
  19. }
  20. }
  21. }

运行结果:


   
   
  1. A-1
  2. B-1
  3. B-2
  4. B-3
  5. A-2
  6. B-4
  7. A-3
  8. B-5
  9. A-4
  10. A-5
可以看出A线程和B线程是交替执行的。

而在其中加入join()方法后(后面的代码都略去了ThreadTest类的定义)


   
   
  1. package CSDN;
  2. public class TestJoin {
  3. public static void main(String[] args) throws InterruptedException {
  4. // TODO Auto-generated method stub
  5. ThreadTest t1= new ThreadTest( "A");
  6. ThreadTest t2= new ThreadTest( "B");
  7. t1.start();
  8. t1.join();
  9. t2.start();
  10. }
  11. }

运行结果:


   
   
  1. A-1
  2. A-2
  3. A-3
  4. A-4
  5. A-5
  6. B-1
  7. B-2
  8. B-3
  9. B-4
  10. B-5

显然,使用t1.join()之后,B线程需要等A线程执行完毕之后才能执行。需要注意的是,t1.join()需要等t1.start()执行之后执行才有效果,此外,如果t1.join()放在t2.start()之后的话,仍然会是交替执行,然而并不是没有效果,这点困扰了我很久,也没在别的博客里看到过。

为了深入理解,我们先看一下join()的源码。


   
   
  1. /**
  2. * Waits for this thread to die.
  3. *
  4. * <p> An invocation of this method behaves in exactly the same
  5. * way as the invocation
  6. *
  7. * <blockquote>
  8. * {@linkplain #join(long) join}{@code (0)}
  9. * </blockquote>
  10. *
  11. * @throws InterruptedException
  12. * if any thread has interrupted the current thread. The
  13. * <i>interrupted status</i> of the current thread is
  14. * cleared when this exception is thrown.
  15. */
  16. public final void join() throws InterruptedException {
  17. join( 0);             //join()等同于join(0)
  18. }
  19. /**
  20. * Waits at most {@code millis} milliseconds for this thread to
  21. * die. A timeout of {@code 0} means to wait forever.
  22. *
  23. * <p> This implementation uses a loop of {@code this.wait} calls
  24. * conditioned on {@code this.isAlive}. As a thread terminates the
  25. * {@code this.notifyAll} method is invoked. It is recommended that
  26. * applications not use {@code wait}, {@code notify}, or
  27. * {@code notifyAll} on {@code Thread} instances.
  28. *
  29. * @param millis
  30. * the time to wait in milliseconds
  31. *
  32. * @throws IllegalArgumentException
  33. * if the value of {@code millis} is negative
  34. *
  35. * @throws InterruptedException
  36. * if any thread has interrupted the current thread. The
  37. * <i>interrupted status</i> of the current thread is
  38. * cleared when this exception is thrown.
  39. */
  40. public final synchronized void join(long millis) throws InterruptedException {
  41. long base = System.currentTimeMillis();
  42. long now = 0;
  43. if (millis < 0) {
  44. throw new IllegalArgumentException( "timeout value is negative");
  45. }
  46. if (millis == 0) {
  47. while (isAlive()) {
  48. wait( 0);            //join(0)等同于wait(0),即wait无限时间直到被notify
  49. }
  50. } else {
  51. while (isAlive()) {
  52. long delay = millis - now;
  53. if (delay <= 0) {
  54. break;
  55. }
  56. wait(delay);
  57. now = System.currentTimeMillis() - base;
  58. }
  59. }
  60. }

可以看出,join()方法的底层是利用wait()方法实现的。可以看出,join方法是一个同步方法,当主线程调用t1.join()方法时,主线程先获得了t1对象的锁,随后进入方法,调用了t1对象的wait()方法,使主线程进入了t1对象的等待池,此时,A线程则还在执行,并且随后的t2.start()还没被执行,因此,B线程也还没开始。等到A线程执行完毕之后,主线程继续执行,走到了t2.start(),B线程才会开始执行。

此外,对于join()的位置和作用的关系,我们可以用下面的例子来分析


   
   
  1. package CSDN;
  2. public class TestJoin {
  3. public static void main(String[] args) throws InterruptedException {
  4. // TODO Auto-generated method stub
  5. System.out.println(Thread.currentThread().getName()+ " start");
  6. ThreadTest t1= new ThreadTest( "A");
  7. ThreadTest t2= new ThreadTest( "B");
  8. ThreadTest t3= new ThreadTest( "C");
  9. System.out.println( "t1start");
  10. t1.start();
  11. System.out.println( "t2start");
  12. t2.start();
  13. System.out.println( "t3start");
  14. t3.start();
  15. System.out.println(Thread.currentThread().getName()+ " end");
  16. }
  17. }

运行结果为


   
   
  1. main start
  2. t1start
  3. t1end
  4. t2start
  5. t2end
  6. t3start
  7. t3end
  8. A- 1
  9. A- 2
  10. main end
  11. C- 1
  12. C- 2
  13. C- 3
  14. C- 4
  15. C- 5
  16. A- 3
  17. B- 1
  18. B- 2
  19. B- 3
  20. B- 4
  21. B- 5
  22. A- 4
  23. A- 5

A、B、C和主线程交替运行。加入join()方法后


   
   
  1. package CSDN;
  2. public class TestJoin {
  3. public static void main(String[] args) throws InterruptedException {
  4. // TODO Auto-generated method stub
  5. System.out.println(Thread.currentThread().getName()+ " start");
  6. ThreadTest t1= new ThreadTest( "A");
  7. ThreadTest t2= new ThreadTest( "B");
  8. ThreadTest t3= new ThreadTest( "C");
  9. System.out.println( "t1start");
  10. t1.start();
  11. System.out.println( "t1end");
  12. System.out.println( "t2start");
  13. t2.start();
  14. System.out.println( "t2end");
  15. t1.join();
  16. System.out.println( "t3start");
  17. t3.start();
  18. System.out.println( "t3end");
  19. System.out.println(Thread.currentThread().getName()+ " end");
  20. }
  21. }

运行结果:


   
   
  1. main start
  2. t1start
  3. t1end
  4. t2start
  5. t2end
  6. A- 1
  7. A- 2
  8. A- 3
  9. A- 4
  10. A- 5
  11. B- 1
  12. t3start
  13. B- 2
  14. t3end
  15. main end
  16. B- 3
  17. B- 4
  18. B- 5
  19. C- 1
  20. C- 2
  21. C- 3
  22. C- 4
  23. C- 5

多次实验可以看出,主线程在t1.join()方法处停止,并需要等待A线程执行完毕后才会执行t3.start(),然而,并不影响B线程的执行。因此,可以得出结论,t.join()方法只会使主线程进入等待池并等待t线程执行完毕后才会被唤醒。并不影响同一时刻处在运行状态的其他线程。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值