package thread;
public class JoinTest {
public static void main(String[] args)throws Exception{
System.out.println("main thread");
Thread thread = new Thread(new SleepThread());
thread.start();
thread.join();
System.out.println("main Thread end");
}
public static class SleepThread implements Runnable{
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + "end!");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
可以看到主线程等thread线程执行完毕后才执行最后一条print语句