join
public final void join() throws InterruptedException {
join(0);
}
该方法会一直等待直到调用的方法结束
public final synchronized void join(long millis)throws InterruptedException {}
该方法可以指定终止的最长时间
测试方法示例 1
public class MyThead implements Runnable{
Thread t;
public MyThead(String threadName){
t = new Thread(this,threadName);
System.out.println(t);
t.start();
}
@Override
public void run() {
for(int i=0;i<5;i++){
try {
System.out.println(t.getName() + "\t:\t" + i);
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
MyThead m1 = new MyThead("one");
m1.t.join();
System.out.println("m thread is over");
输出
Thread[one,5,main]
one : 0
one : 1
one : 2
one : 3
one : 4
m thread is over
测试方法示例 2
MyThead m1 = new MyThead("one");
m1.t.join(1000);
System.out.println("m thread is over");
输出
Thread[one,5,main]
one : 0
one : 1
m thread is over
one : 2
one : 3
one : 4