线程的强制执行
-
Join合并线程,待此线程执行完成后,再执行其他线程,其他线程阻塞
-
可以理解为插队
-
join()
等待这个线程死亡。 -
void join(long millis)
等待这个线程死亡最多 millis毫秒。 -
void join(long millis, int nanos)
等待最多 millis毫秒加上 nanos纳秒这个线程死亡。
代码示例
package com.thread;
public class TestJoin implements Runnable {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
}
}
public static void main(String[] args) {
TestJoin testJoin = new TestJoin();
Thread t1 = new Thread(testJoin);
t1.start();
for (int i = 0; i < 500; i++) {
System.out.println(Thread.currentThread().getName()+"--->"+i);
if (i == 2){
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}