使用join()
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子正在执行");
},"子线程");
thread.start();
thread.join();
System.out.println("主线程执行" );
}
使用CountdownLatch
CountDownLatch c = new CountDownLatch(1);
Thread thread = new Thread(() -> {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("子正在执行");
c.countDown();
},"子线程");
thread.start();
c.await();
System.out.println("主线程执行" );
}
使用线程池的while (submit.isDone())
public static void main(String[] args) throws InterruptedException, ExecutionException {
ExecutorService executorService = Executors.newFixedThreadPool(1);
Thread