join
当前线程等待join的线程执行完才执行
下面的例子是:当前线程是main线程,join的线程是a线程
public class JoinTest { public static void main(String[] args) throws Exception { Thread a = new ThreadA(); a.start(); // 等待a线程执行完后,主线程才继续执行 // a.join(); // 我只等待3秒,不管a线程是否执行完,主线程都会继续执行 a.join(3_000); System.out.println("##########"); } } class ThreadA extends Thread { @Override public void run() { try { Thread.sleep(10_000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("ThreadName = " + Thread.currentThread().getName()); } }