Thread 之 join() 方法
先看一段代码,下面 count 的值是多少?
@Slf4j
public class ThreadDemo {
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
FutureTask<String> futureTask = new FutureTask(() -> {
log.info(Thread.currentThread().getName() + " 开始运行");
count = 10;
TimeUnit.SECONDS.sleep(3);
log.info(Thread.currentThread().getName() + " 运行结束");
return "success";
});
log.info(Thread.currentThread().getName() + " 开始运行");
Thread t1 = new Thread(futureTask, "t1");
t1.start();
count++;
log.info("count: {}", count);
log.info(Thread.currentThread().getName() + " 运行结束");
}
}
分析一下结果
主线程开始运行,与此同时 t1 线程也开始运行,t1 运行过程中调用 sleep() 方法,t1 线程阻塞,main 线程获取到 CPU 时间片执行后续代码,count++,最终结果为 1
但是如果我想要等 t1 线程执行完毕之后才执行主线程的代码,那么该怎么做呢?
这个时候就需要使用 join(...) 方法,join 方法的作用就是等待其它线程运行结束
join 有两个重载方法,分别是 join() 和 join(long millis),但是实际上底层都是使用同一个方法
看一下 join() 的具体实现
public final void join() throws InterruptedException {
// 调用的是 join(long millis) 这个方法,只是时间为 0
join(0);
}
接着再看一下 join(long millis) 这个方法
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");
}
// 如果 millis 的值为 0,底层调用 wait() 方法一直等待,如果没有调用 notify() 或 notifyAll() 方法,线程将一直等待
if (millis == 0) {
while (isAlive()) {
wait(0);
}
// millis 的值不为 0 的时候,会判断当前线程是否存活,如果存活的话会计算剩余等待时间,过时不候,底层也是调用 wait(delay),delay 时间到了之后会被唤醒
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
看完了代码之后,继续来看一下如何使用 join() 方法
@Slf4j
public class ThreadDemo {
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
FutureTask<String> futureTask = new FutureTask(() -> {
log.info(Thread.currentThread().getName() + " 开始运行");
count = 10;
TimeUnit.SECONDS.sleep(3);
log.info(Thread.currentThread().getName() + " 运行结束");
return "success";
});
log.info(Thread.currentThread().getName() + " 开始运行");
Thread t1 = new Thread(futureTask, "t1");
t1.start();
// 在 t1 线程启动之后,在 main 线程中调用 t1 线程的 join 方法,那么 main 线程会等待 t1 线程运行结束,然后 main 线程再执行剩余的代码
t1.join();
count++;
log.info("count: {}", count);
log.info(Thread.currentThread().getName() + " 运行结束");
}
}
从上面的结果可以看出,在 main 线程中,使用 t1.join() 方法,那么 main 线程会等待 t1 线程执行结束后,main 线程再执行剩余的代码
当有多个线程 join 时,又是怎样的结果呢?
@Slf4j
public class ThreadDemo {
private static int count = 0;
public static void main(String[] args) throws InterruptedException {
FutureTask<String> futureTask = new FutureTask(() -> {
log.info(Thread.currentThread().getName() + " 开始运行");
TimeUnit.SECONDS.sleep(3);
count = 10;
log.info(Thread.currentThread().getName() + " 运行结束");
return "success";
});
FutureTask<String> futureTask2 = new FutureTask(() -> {
log.info(Thread.currentThread().getName() + " 开始运行");
TimeUnit.SECONDS.sleep(5);
count = 20;
log.info(Thread.currentThread().getName() + " 运行结束");
return "success";
});
log.info(Thread.currentThread().getName() + " 开始运行");
Thread t1 = new Thread(futureTask, "t1");
Thread t2 = new Thread(futureTask2, "t2");
t1.start();
t2.start();
// 同时等待 t1、t2 执行完毕
t1.join();
t2.join();
count++;
log.info("count: {}", count);
log.info(Thread.currentThread().getName() + " 运行结束");
}
}
从上面的结果可以看出,main 线程开始到结束运行了 5 s,而 t1 线程等待 3 s,t2 线程等待 5s,main 线程实际等待的时间不是 8s,说明 t1 线程和 t2 线程是并行执行的