引言
join方法的实现逻辑
首先,我们来看jdk中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");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
可以发现它是基于Object.wait()方法实现的,因为wait()方法的调用前提是持有对象锁,所以join()方法被synchronized修饰。调用wait方法后,线程会处于WAITING状态而不会继续运行。
这里有一个不太好理解的地方,假设当前有两个线程:主线程和线程A,主线程调用线程A对象的join方法,那么哪个线程会继续运行,哪个线程会处于WAITING状态?看下面的例子:
public class JoinTest {
public static void main(String[] args) throws InterruptedException {
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
while (true){
System.out.println("threadA is running");
}
}
},"threadA");
threadA.start();
threadA.join();
System.out.println("the main thread is running");
}
}
在这个例子中,主线程(main线程)调用了threadA的join方法,运行时,会一直输出threadA is running,而不会输出the main thread is running。说明处于WAITING状态的是主线程。
为什么会这样?我们从join方法的实现来分析:
首先main线程调用threadA对象的join方法,获取到了threadA的对象锁,然后调用wait()方法,实际上调用的是threadA这个Thread实例对象的wait方法,所以main线程会释放持有的threadA对象锁,然后进入WAITING状态。其实整个逻辑比较简单,只是对象锁是在一个代表线程threadA的对象上面,所以有些迷惑。
join的唤醒
根据join方法的逻辑,我们看到,当参数是0时,执行的是wait(0)方法,执行之后线程会一直处于WAITING状态直到其他线程执行了notify或者notifyAll方法才会被唤醒,但是join方法里面并没有notify的逻辑,那么调用join方法的线程就一直WAITING下去,不能被唤醒了吗?不是的,当使其处于WAITING的thread对象代表的线程执行完成后,该线程就会被唤醒,看下面的例子:
public class JoinTest {
public static void main(String[] args) throws InterruptedException {
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<100;i++){
System.out.println(i);
}
}
},"threadA");
threadA.start();
threadA.join(0);
System.err.println("the main thread is running");
}
}
主线程调用threadA的join方法,在threadA对象锁上处于WAITING状态,在threadA线程执行完成后,就会被唤醒,输出如下:
0
1
2
3
4
5
6
7
8
9
the main thread is running
如果join方法的参数不为零呢?它执行的是wait(对应的时间),也就是当对应的时间过去之后,处于WAITING状态的线程会自动唤醒从而继续执行,而不需要等到threadA执行完成,看下面的例子:
public class JoinTest {
public static void main(String[] args) throws InterruptedException {
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10000);
System.out.println("the threadA finished sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"threadA");
threadA.start();
threadA.join(3000);
System.err.println("the main thread is running");
}
}
threadA睡眠10秒钟,join的参数是3秒钟,3秒钟之后main方法就会执行,输出如下:
the main thread is running
the threadA finished sleep
join的使用
因为join方法可以让一个线程在另外一个线程运行完成之后再运行,所以可以用来安排不同线程的执行顺序,看下面的例子:
public class Join extends Thread {
private Thread previousThread;
private int order;
public Join(Thread previousThread,int order) {
this.previousThread = previousThread;
this.order = order;
}
@Override
public void run() {
try {
previousThread.join(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(this.order);
}
public static void main(String[] args) throws InterruptedException {
Thread previousThread = Thread.currentThread();
for(int i = 0; i<100; i++){
Join join = new Join(previousThread,i);
join.start();
previousThread = join;
}
}
}
这个例子中,每个线程都在前一个线程运行完成之后再运行,结果会按顺序输出0到99。
小结
Thread.join()方法的实现比较有迷惑性并且在实际中使用的并不多,我们明白了它的实现原理之后能够用它的特性实现一些对线程执行前后顺序有要求的功能。