join()函数会等待线程处理完事情,才继续处理其他事情
有关于join 方法的使用
public class Thread1 {
public static void main(String[] args) {
Thread thread = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("child threadOne over!");
});
Thread thread1 = new Thread(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("child threadTwo over!");
});
// 启动子线程
thread.start();
thread1.start();
System.out.println("wait all child thread over !");
try {
// 等待子线程执行完毕,返回
thread.join();
thread1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("all child thread over !");
}
}