Thread中的join方法主要的作用是让jion的线程加入当前线程,等加入的线程执行完之后才会执行当前线程。
接下来看个例子:
public class TestJoin {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() ->
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("this is t1");
});
Thread t2 = new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("this is t2");
});
Thread t3 = new Thread(() -> {
System.out.println("this is t3");
});
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println("运行结束");
}
}
输出结果为:
this is t3
this is t1
this is t2
运行结束
可以看到输出是线程t1,t2,t3输出之后,才会输出到主线程。所以可以看出来join方法是把其他线程加入当前线程,等加入线程执行完之后才会执行当前线程。
如果想要t1,t2,t3再顺序输出,只需要在每个start方法后面再调用join就行了。这就就保证了每次主线程运行完一个线程,再去运行另外一个线程。
修改上面代码为:
public class TestJoin {
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(() ->
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("this is t1");
});
Thread t2 = new Thread(() -> {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("this is t2");
});
Thread t3 = new Thread(() -> {
System.out.println("this is t3");
});
t1.start();
t1.join();
t2.start();
t2.join();
t3.start();
t3.join();
System.out.println("运行结束");
}
}
输出为:
this is t1
this is t2
this is t3
运行结束
接下来我们再从源码分析下Thread的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;
}
}
}
由源码可以看到,其实底层就是wait方法,并且有一个while死循环,直到isAlive是False,也就是线程执行完成或者不存活。
顺带一提下Thread的yield的方法,此方法其实是只当前线程让出CPU回到就绪状态,线程再重新争抢CPU,但是结果可能是其他线程抢到CPU,也可能是自己抢到CPU。