执行某个线程thread的join方法,表示当前线程需要等待thread线程执行完成后再继续执行。
public class JoinMethod implements Runnable {
@Override
public void run() {
System.out.println("Sub thread start, thread name is " + Thread.currentThread().getName());
for (int i = 0; i < 10; i++) {
System.out.println("i = " + i);
}
System.out.println("Sub thread end, thread name is " + Thread.currentThread().getName());
}
public static void main(String[] args) throws InterruptedException {
System.out.println("Main thread start, thread name is " + Thread.currentThread().getName());
Thread thread = new Thread(new JoinMethod());
thread.start();
thread.join();
System.out.println("Main thread end, thread name is " + Thread.currentThread().getName());
}
}