java线程之join方法实现等待机制

java线程之join方法实现等待机制

大白话:一个线程A中调用了另一个线程对象B的join方法,A会等待B执行完毕后再执行后面的代码;

简介:

如果一个线程A执行了thread.join()语句,其含义是:当前线程A等待thread线程终止之后才从thread.join()返回。线程Thread除了提供join()方法之外,还提供了join(long millis)和join(longmillis,int nanos)两个具备超时特性的方法。这两个超时方法表示,如果线程thread在给定的超时时间里没有终止,那么将会从该超时方法中返回。

dmeo代码示例:

package com.demo.other;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;

public class
ThreadDemo {
    public static void main(String[] args) throws InterruptedException {
        ThreadDemo threadDemo = new ThreadDemo();
        threadDemo.test();
    }

    public void test() throws InterruptedException {
        Thread t = new Thread(() -> {
            try {
                System.out.println("子线程中:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss SSS")));
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        t.start();
        t.join();
        System.out.println("完毕:"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm:ss SSS")));
    }
}

打印:

子线程中:14:53:12 244
完毕:14:53:17 267

源码分析

  1. t.join();是在主线程中调用了子线程对象t的join方法;

  2. 进入join方法

        public final void join() throws InterruptedException {
            join(0);
        }
    
  3. 进入join(0)方法

    //注意,这里是synchronized 修饰的同步方法,不然里面不能使用wait,会抛出非法监视器状态异常
     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) {
            	//判断子线程t是否活着,活着就等待,收到通知后再验证是否活着
                while (isAlive()) {
                //这里实际上是调用了子线程对象t的wait(0)方法,进行无限等待,t对象是main线程中,所以main线程等待。
                    wait(0);
                }
            } else {
                while (isAlive()) {
                    long delay = millis - now;
                    if (delay <= 0) {
                        break;
                    }
                    wait(delay);
                    now = System.currentTimeMillis() - base;
                }
            }
        }
    
    
  4. 既然main线程调用了子线程对象的wait方法,那么,在哪里调用了子线程对象的notify或notifyAll方法呢?
    经过查资料得知,当线程终止时,会调用线程对象自身的notifyAll()方法,会通知所有等待在该线程对象上的线程。
    但这个操作并不在java源码中,而是在native方法中,会在run方法执行完毕时调用自身的notifyAll方法;

    在 run 方法执行结束之前会调用 lock.notify_all(thread) 通知所有 join 等待的线程。源码如下:
    
    /jdk7/hotspot/src/share/vm/runtime/thread.cpp
    
    void JavaThread::run() {
    ...
    thread_main_inner();
    }
    
    void JavaThread::thread_main_inner() {
    ...
    this->exit(false);
    delete this;
    }
    
    void JavaThread::exit(bool destroy_vm, ExitType exit_type) {
    ...
    // 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);
    ...
    }
    
    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
    java_lang_Thread::set_thread(threadObj(), NULL);
    //在这里进行通知的
    lock.notify_all(thread);
    // Ignore pending exception (ThreadDeath), since we are exiting anyway
    thread->clear_pending_exception();
    }
    

总结:join方法是通过wait/notify机制来实现等待线程结束的;

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值