1. join简介
join()方法是Thread类中的一个方法,该方法的定义是等待该线程终止。其实就是join()方法将挂起调用线程的执行,直到被调用的对象完成它的执行。
这句话讲的很抽象,简单来说就是父线程等待子线程结束后才能继续运行。
2. join示例
先来看一下这段简单的代码:
public class test {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(() -> {
System.out.println("thread1");
});
Thread thread2 = new Thread(() -> {
System.out.println("thread2");
});
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println("main");
}
}
执行结果:
thread1
thread2
main
经过过次测试,可能出现会出现thread2在thread1之前,但是main总是会出现在最后。
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(0)
主要看这段代码块:
while (isAlive()) {
wait(0);
}
当该线程仍在执行的时候调用该线程对象的wait方法,wait(0)表示一直等待直到被唤醒,而wait方法表示使用该对象的线程进入阻塞。
在示例代码中main线程调用了thread1和thread2线程,main是持有这两个线程对象的,因此在thread1和thread2调用join方法后(内部使用了wait方法),main线程进入阻塞态,直到子线程执行结束,main线程才会被唤醒。
3. 思考的问题
- 为什么thread1和thread2两个线程可以并发执行?
thread1和thread2是平级的,Object的wait方法只会让持有该对象的线程阻塞,而thread1和thread2两个线程相互独立,因此两个线程不会进入阻塞。 - main线程是在什么时候唤醒的?
我没有在源码中找到类似notify这种唤醒线程的语句,网上相关博客讲到子线程结束后会调用this.notify()方法唤醒主线程,但是具体在哪里实现我还没找到。。。
参考: