join()让当前线程优先执行。
JoinThread.java
public class JoinThread implements Runnable{
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println("vip"+i);
}
}
}
Test.java
import JavaBase.*;
public class Test {
public static void main(String args[]) throws InterruptedException {
JoinThread joinThread = new JoinThread();
Thread thread = new Thread(joinThread);
thread.start();
for (int i = 0; i < 100; i++) {
if (i == 80) {
thread.join(1);
}
System.out.println("main" + i);
}
}
}
运行结果
程序执行到thread.join(1);其它程序终止,执行该线程。