java多线程中对于join方法的理解

对于多线程的join方法一直理解得很模糊,大致看了一些资料,JDK是这样说的:join public final void join (long millis )throws InterruptedException Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever. 字面意思是join用于主线程等待子线程运行完毕它的run方法,再继续执行下面的代码。例如:

public class JoinTest {

    public static void main(String[] args) {
        Thread t =  new  Thread( new  RunnableImpl());
        t.start();
        try {
           t.join();//t线程完成run方法后才会继续执行下面的代码!
        }  catch  (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Thread t1 =  new  Thread( new  RunnableImpl1());
        t1.start();
    }

就是说如果是t.join() = t.join(0) JDK 这样说的 A timeout of 0 means to wait forever 字面意思是永远等待,其实是等到t 结束后。

JDK源码如下:

public final synchronized void join(long millis)
    throws InterruptedException {
        long base = System.currentTimeMillis();
        long now = 0;

        if (millis < 0) {
            throw new IllegalArgumentException("timeout value is negative");
        }

        if (millis == 0) {
            while (isAlive()) {
                wait(0);
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

其实Join 方法实现是通过wait (小提示:Object 提供的方法)。 当main 线程调用t.join 时候,main 线程会获得线程对象t 的锁 (wait 意味着拿到该对象的锁), 调用该对象的wait( 等待时间) ,直到该对象唤醒main 线程,比如退出后。
这就意味着main 线程调用t.join 时,必须能够拿到线程t 对象的锁 ,如果拿不到它是无法wait 的,刚开的例子t.join(1000) 不是说明了main 线程等待1 秒,如果在它等待之前,其他线程获取了t 对象的锁,它等待时间可不就是1 毫秒了。上代码介绍:

public   class  JoinTest { 
    public   static   void  main(String[] args) { 
    Thread t =  new  Thread( new  RunnableImpl()); 
    new  ThreadTest(t).start();// 这个线程会持有锁 
    t.start(); 
    try  { 
    t.join(); 
    System.out.println("joinFinish"); 
    }  catch  (InterruptedException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    } 
    } 
    } 
    class  ThreadTest  extends  Thread { 
    Thread thread; 
    public  ThreadTest(Thread thread) { 
    this .thread = thread; 
    } 
    @Override 
    public   void  run() { 
    holdThreadLock(); 
    } 
    public   void  holdThreadLock() { 
    synchronized  (thread) { 
    System.out.println("getObjectLock"); 
    try  { 
    Thread.sleep(9000); 
    }  catch  (InterruptedException ex) { 
    ex.printStackTrace(); 
    } 
    System.out.println("ReleaseObjectLock"); 
    } 
    } 
    } 
    class  RunnableImpl  implements  Runnable { 
    @Override 
    public   void  run() { 
    try  { 
    System.out.println("Begin sleep"); 
    Thread.sleep(2000); 
    System.out.println("End sleep"); 
    }  catch  (InterruptedException e) { 
    e.printStackTrace(); 
    } 
    } 
    } 

在main 方法中 通过new ThreadTest(t).start(); 实例化ThreadTest 线程对象, 它在 holdThreadLock() 方法中,通过 synchronized (thread) ,获取线程对象t 的锁,并Sleep (9000 )后释放,这就意味着,即使
main 方法t.join(1000), 等待一秒钟,它必须等待 ThreadTest 线程释放t 锁后才能进入wait 方法中,它实际等待时间是9000+1000 MS

运行结果是:
getObjectLock
Begin sleep
End sleep
ReleaseObjectLock
joinFinish

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值