Java Thread的join() 原理

join

线程的合并的含义就是 将几个并行线程的线程合并为一个单线程执行,应用场景是 当一个线程必须等待另一个线程执行完毕才能执行时,Thread类提供了join方法来完成这个功能,注意,它不是静态方法。

join有3个重载的方法:

void join():当前线程等该加入该线程后面,等待该线程终止。
void join(long millis):当前线程等待该线程终止的时间最长为 millis 毫秒。 如果在millis时间内,该线程没有执行完,那么当前线程进入就绪状态,重新等待cpu调度。
void join(long millis,int nanos):等待该线程终止的时间最长为 millis 毫秒 + nanos纳秒。如果在millis时间内,该线程没有执行完,那么当前线程进入就绪状态,重新等待cpu调度。

我们来看个demo:
新建一个Thread类,重写run()方法:

public class MyThread extends Thread {

    @Override
    public void run() {
        System.out.println("子线程执行完毕");
    }
}

新建测试类,测试Join()方法:

public class TestThread {

    public static void main(String[] args) {
        //循环五次
        for (int i = 0; i < 5; i++) {

            MyThread thread = new MyThread();
            //启动线程
            thread.start();
            try {
                //调用join()方法
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("主线程执行完毕");
            System.out.println("~~~~~~~~~~~~~~~");

        }
    }
}

子线程执行完毕
主线程执行完毕
~~~~~~~~~~~~~~~
子线程执行完毕
主线程执行完毕
~~~~~~~~~~~~~~~
子线程执行完毕
主线程执行完毕
~~~~~~~~~~~~~~~
子线程执行完毕
主线程执行完毕
~~~~~~~~~~~~~~~
子线程执行完毕
主线程执行完毕
~~~~~~~~~~~~~~~

结果分析: 子线程每次都在主线程之前执行完毕.

源码解读

public final void join() throws InterruptedException {
    //当调用join()时,实际是调用join(long)方法
        join(0);
    }

查看Join(long)方法源码:

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) {  //由于上一步传入参数为0,因此调用当前判断
            while (isAlive()) { //判断子线程是否存活
                wait(0); //调用wait(0)方法
            }
        } else {
            while (isAlive()) {
                long delay = millis - now;
                if (delay <= 0) {
                    break;
                }
                wait(delay);
                now = System.currentTimeMillis() - base;
            }
        }
    }

/**
     * Tests if this thread is alive. A thread is alive if it has
     * been started and has not yet died.
     * 测试线程是否还活着。如果线程存活的话它就是已经开始,还没有死亡的状态。
     * @return  true if this thread is alive;
     *          false otherwise.
     */****
    public final native boolean isAlive();

该方法为本地方法,判断线程对象是否存活,若线程对象调用了start()方法,在没有死亡的情况下此判断为true。

A thread is alive if it has been started and has not yet died. A thread "starts" when its start() method is invoked and "dies" at the end of its run() method, or when stop() (now deprecated) is invoked. So yes, a thread is "alive" when its run() method is still ongoing, but it is also "alive" in the time window between the invocation of start() and the implicit invocation of the run() method by the JVM.

查看wait()方法源码:

public final native void wait(long timeout) throws InterruptedException;

该方法为本地方法,调用此方法的当前线程需要释放锁,并等待唤醒。在上述例子中,主线程调用子线程对象的join()方法,因此主线程在此位置需要释放锁,并进行等待。

我们来小结下上述步骤: 主线程wait()等待,子线程调用了run()执行,打印“子线程执行完毕”。此时,主线程还没被唤醒,还没有执行下面的操作。那么,问题来了,谁?在什么时候?唤醒了主线程呢?
在stackoverflow上 找到
The notify() for this is handled by the Thread subsystem. When the run() method finishes, the notify() is called on the Thread object. I'm not sure if the code that actually calls notify() can be seen -- it seems to be done in native code.

No user code needs to call notify() on that Thread object. The Java Thread code handles this internally. Once the thread finishes, the join() call will return.

As for jdk7 for linux, you can get the answer from the source code of openjdk.

/jdk7/hotspot/src/os/linux/vm/os_linux.cpp

int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);

static void *java_start(Thread *thread) {
  ...
  thread->run();
  return 0;
}

and when start thread in java, the thread will be instanceof JavaThread.

/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();
}

so lock.notify_all(thread) will notify all threads whose wait for the thread to finish.

你可能感兴趣的:(Java Thread的join() 原理)