Thread.join()详解
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");
}
//判断是否携带阻塞的超时时间,等于0表示没有设置超时时间
if (millis == 0) {
//isAlive获取线程状态,无线程等待直到 join 的线程结束
while (isAlive()) {
//调用Object中的wait方法实现线程的阻塞
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
wait(0) 当超时时间是 0 时,线程只会等待被唤醒。而不会超过过期时间后自动唤醒
join 方法的本质调用的是 Object 中的 wait() 方法实现线程的阻塞。调用wait方法必须要获取锁,所以 join 方法是被 synchronized 修饰的,synchronized 修饰在方法层面相当于synchronized(this),this 就是当前线程的实例
例子:
public class JoinDemo extends Thread{
int i;
Thread previousThread;
public JoinDemo(Thread previousThread,int i){
this.previousThread=previousThread;
this.i=i;
}
@Override
public void run() {
try {
previousThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("num:"+i);
}
public static void main(String[] args) {
Thread previousThread=Thread.currentThread();
for(int i=0;i<10;i++){
JoinDemo joinDemo=new JoinDemo(previousThread,i);
joinDemo.start();
previousThread=joinDemo;
}
}
}
join() 方法里面调用 wait() 方法阻塞的是主线程,注意:阻塞的是主线程,阻塞的是主线程,阻塞的是主线程
因为主线程会持有 previousThread 这个对象的锁,然后调用 wait 方法去阻塞,而**这个方法的调用者是在主线程中**的。所以造成主线程阻塞
主线程何时被唤醒?
通过 wait 方法阻塞的线程,需要通过 notify 或者 notifyall 来唤醒。所以在线程执行完毕以后一定会有一个唤醒的操作
在 Hotspot的源码中找到 thread.cpp
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();
}
ensure_join 方法中,调用 lock.notify_all(thread); 唤醒所有等待 Thread 锁的线程,意味着调用了join方法被阻塞的主线程会被唤醒
总结
Thread.join 其实底层是通过 wait/notifyall 来实现线程的通信达到线程阻塞的目的;当线程执行结束以后,会触发两个事情:
- 第一个是设置
native线程对象为null - 第二个是通过
notifyall方法,让阻塞的主线程被唤醒
本文详细解析了Thread.join()方法的工作原理,包括如何使用wait/notifyall实现线程间的同步阻塞,以及主线程如何被正确唤醒的过程。同时,提供了一个具体的示例帮助理解。
1492

被折叠的 条评论
为什么被折叠?



