线程的 join 方法 API 说明,
Waits for this thread to die.
An invocation of this method behaves in exactly the same way as the invocation
join(0)
Throws:
InterruptedException – if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.
public final void join() throws InterruptedException {
join(0);
}
翻译过来就是:
等待这个线程死亡。
此方法的调用行为与 join(0) 调用完全相同
抛出 InterruptedException 异常 – 如果任何线程中断了当前线程。 抛出此异常时清除当前线程的中断状态。
嗯,没看明白。
查了资料,结合 API 说明,意思是:
假设有 A 、 B 两个线程,要求 B 先执行完毕,然后再执行 A 。
简言之,B 优先执行,A 在 B 执行完再执行。
举个栗子,
public class JoinThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread aThread = new Thread("aThread"){
@Override
public void run() {
super.run();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " finish");
}
};
aThread.start();
System.out.println(Thread.currentThread().getName() + " finish");
}
}
创建一个 Thread ,添加延时,
运行结果,
main finish
aThread finish
如果我想让 aThread 优先执行,就用 join 方法,示例,
public class JoinThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread aThread = new Thread("aThread"){
@Override
public void run() {
super.run();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " finish");
}
};
aThread.start();
aThread.join(); //添加这行
System.out.println(Thread.currentThread().getName() + " finish");
}
}
运行结果,符合预期
aThread finish
main finish
新建 3 个线程试试,都不用 join 方法,
public class JoinThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(){
@Override
public void run() {
super.run();
for (int i = 0 ; i < 6; i++){
System.out.println("thread1 running , i = " + i);
}
System.out.println("thread1 finish");
}
};
Thread thread2 = new Thread(){
@Override
public void run() {
super.run();
for (int i = 0 ; i < 6; i++){
System.out.println("thread2 running , i = " + i);
}
System.out.println("thread2 finish");
}
};
Thread thread3 = new Thread(){
@Override
public void run() {
super.run();
for (int i = 0 ; i < 6; i++){
System.out.println("thread3 running , i = " + i);
}
System.out.println("thread3 finish");
}
};
thread1.start();
thread2.start();
thread3.start();
}
运行结果,
thread3 running , i = 0
thread3 running , i = 1
thread3 running , i = 2
thread3 running , i = 3
thread3 running , i = 4
thread3 running , i = 5
thread3 finish
thread1 running , i = 0
thread1 running , i = 1
thread1 running , i = 2
thread1 running , i = 3
thread1 running , i = 4
thread1 running , i = 5
thread1 finish
thread2 running , i = 0
thread2 running , i = 1
thread2 running , i = 2
thread2 running , i = 3
thread2 running , i = 4
thread2 running , i = 5
thread2 finish
可以看到,3 个线程的运行顺序是不固定的,多运行几次,结果是不一样的。
使用 join 方法,
thread2 中调用 thread1.join() ,确保 thread1 执行完后再执行 thread2 ,
thread3 中调用 thread2.join() , 确保 thread2 执行完后再执行 thread3 ,
public class JoinThreadTest {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(){
@Override
public void run() {
super.run();
for (int i = 0 ; i < 6; i++){
System.out.println("thread1 running , i = " + i);
}
System.out.println("thread1 finish");
}
};
Thread thread2 = new Thread(){
@Override
public void run() {
super.run();
try {
thread1.join();// thread1 优先执行,thread2 等待 thread1 执行完再执行
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0 ; i < 6; i++){
System.out.println("thread2 running , i = " + i);
}
System.out.println("thread2 finish");
}
};
Thread thread3 = new Thread(){
@Override
public void run() {
super.run();
try {
thread2.join();// thread2 优先执行,thread3 等待 thread2 执行完再执行
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0 ; i < 6; i++){
System.out.println("thread3 running , i = " + i);
}
System.out.println("thread3 finish");
}
};
thread1.start();
thread2.start();
thread3.start();
}
}
多试几次,运行结果都是
thread1 running , i = 0
thread1 running , i = 1
thread1 running , i = 2
thread1 running , i = 3
thread1 running , i = 4
thread1 running , i = 5
thread1 finish
thread2 running , i = 0
thread2 running , i = 1
thread2 running , i = 2
thread2 running , i = 3
thread2 running , i = 4
thread2 running , i = 5
thread2 finish
thread3 running , i = 0
thread3 running , i = 1
thread3 running , i = 2
thread3 running , i = 3
thread3 running , i = 4
thread3 running , i = 5
thread3 finish
说明 join 方法生效了。