Thread.join()详解

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;}@Overridepublic 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 anywaythread->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()方法返回falsejava_lang_Thread::set_thread(threadObj(), NULL);lock.notify_all(thread);//注意这里// Ignore pending exception (ThreadDeath), since we are exiting anywaythread->clear_pending_exception();
}

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

总结

Thread.join 其实底层是通过 wait/notifyall 来实现线程的通信达到线程阻塞的目的;当线程执行结束以后,会触发两个事情:

  • 第一个是设置 native 线程对象为null
  • 第二个是通过 notifyall 方法,让阻塞的主线程被唤醒


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部