线程join() 方法
方法作用
join在线程thread 类中定义,当调用join方法时,当前调用的线程会阻塞,直到被调用线程执行完毕,才继续执行
可以协调多个线程之间的执行顺序,实现线程之间的同步
示例
public class Test {
public static void main(String[] args) throws InterruptedException {
Thread thread1 = new Thread(()->{
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
System.out.println("thread1");
});
thread1.start();
thread1.join();
System.out.println("测试");
}
}
当调用thread1.join()时,main线程会阻塞直到thread1执行完毕