JDK Thread join

博文目录


(四)Thread.join的作用和原理

thread.join() 的作用

谁调用 thread.join() , 谁就会被阻塞, 等待 thread 线程执行结束, 才会被唤醒, 接着 thread.join() 之后继续执行

假设主线程创建了一个子线程 thread, 并且已执行 thread.start(), 然后主线程调用了 thread.join(), 那么主线程会被阻塞, 知道子线程执行结束才会被唤醒, 并继续执行

@Test
@SneakyThrows
public void testJoin() {
    Thread thread = new Thread(() -> {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    // 启动该新子线程
    thread.start();
    // 主线程join
    log.info("在主线程里调用子线程 join 前");
    thread.join();
    log.info("在主线程里调用子线程 join 后");
}
[20220518.172330.108][INFO ][main] 在主线程里调用子线程 join 前
[20220518.172331.109][INFO ][main] 在主线程里调用子线程 join 后

可见主线程在 thread.join() 处被阻塞了一秒

原理

在这里插入图片描述

public final void join() throws InterruptedException {
    join(0);
}
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()? 而不是 主线程.join()?

join 是 Thread 的实例方法, 有 synchronized 修饰符, join 内部实际上是 object.wait(), 调用 wait() 需要先获取锁, 所以 join() 有 synchronized

synchronized 修饰实例方法时, 监视器锁对象 就是 调用该方法的那个实例对象, 即 .join() 前面的 thread 对象

在主线程里执行 thread.join(), 意味着主线程抢到了 thread 这个对象锁, 并由主线程执行其内部的代码, 包括 wait(), 所以主线程会被阻塞

thread 实例仅仅起到了 监视器锁 的作用, 真正执行 join() 内部代码的线程是主线程, 可以通过打断点调试来确认是主线程

wait() 的线程需要 notify() 才能被唤醒, 为什么子线程执行完后主线程会自动被唤醒?

hotspot的源码中找到 thread.cpp

观察一下 ensure_join(this) 这行代码上的注释,唤醒处于等待的线程对象,这个是在线程终止之后做的清理工作

ensure_join 方法中,调用 lock.notify_all(thread); 唤醒所有等待thread锁的线程,意味着调用了join方法被阻塞的主线程会被唤醒

void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
	assert(this == JavaThread::current(),  "thread consistency check");
    ...
    // Notify waiters on thread object. This has to be done after exit() is called
    // on the thread (if the thread is the last thread in a daemon ThreadGroup the
    // group should have the destroyed bit set before waiters are notified).
    ensure_join(this);
    assert(!this->has_pending_exception(), "ensure_join should have cleared");
    ...
}
static void ensure_join(JavaThread* thread) {
    // We do not need to grap the Threads_lock, since we are operating on ourself.
    Handle threadObj(thread, thread->threadObj());
    assert(threadObj.not_null(), "java thread object must exist");
    ObjectLocker lock(threadObj, thread);
    // Ignore pending exception (ThreadDeath), since we are exiting anyway
    thread->clear_pending_exception();
    // Thread is exiting. So set thread_status field in  java.lang.Thread class to TERMINATED.
    java_lang_Thread::set_thread_status(threadObj(), java_lang_Thread::TERMINATED);
    // Clear the native thread instance - this makes isAlive return false and allows the join()
    // to complete once we've done the notify_all below
    // 这里是清除native线程,这个操作会导致isAlive()方法返回false
    java_lang_Thread::set_thread(threadObj(), NULL);
    // 注意这里
    lock.notify_all(thread);
    // Ignore pending exception (ThreadDeath), since we are exiting anyway
    thread->clear_pending_exception();
}

使用

在实际应用开发中,我们很少会使用thread.join。在实际使用过程中,我们可以通过join方法来等待线程执行的结果,其实有点类似future/callable的功能。

public void demo(){
   // ....
   Thread thread = new Thread(payService);
   thread.start();
   // .... 
   // 其他业务逻辑处理,不需要确定thread线程是否执行完
   foo();
   // 后续的处理,需要依赖t线程的执行结果,可以在这里调用join方法等待t线程执行结束
   thread.join();
   // ....
   bar();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值