· Join合并線程,待此線程執行完成后,再執行其他線程,其他線程阻塞。
· 可以想象成插隊。
代碼演示:
//測試Join方法
//想象為插隊
public class TestJoin implements Runnable{@Overridepublic void run() {for (int i = 0; i < 1000; i++) {System.out.println("線程Vip來啦"+i);}}public static void main(String[] args) throws InterruptedException {//啟動線程TestJoin testJoin = new TestJoin();Thread thread = new Thread(testJoin);thread.start();//主線程for (int i = 0; i < 500; i++) {if (i==200){thread.join(); //插隊}System.out.println("main"+i);}}
}