线程Join()
程序员文章站
2022-04-17 18:29:46
...
public class TestJoin {
public static void main(String[] args) {
Runnable3 r3 = new Runnable3();
Thread t = new Thread(r3);
t.start();
try {
t.join();//合并某个线程 等子线程执行完后再执行此代码后的代码,类似方法调用
} catch (InterruptedException e) {
e.printStackTrace();
}
for(int i=0;i<10;i++) {
System.out.println("i am main thread");
}
}
}
public class Runnable3 implements Runnable{
public void run(){
for(int i=0;i<10;i++) {
System.out.println("i am" + Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
}
}
}
}