Join插队
要点:
(1) Join合并线程,待此线程执行完后,再执行其他线程,其他线程阻塞
(2) 可以理解为插队
(3)不建议多使用,会导致线程阻塞
案例代码:
package com.heima.Multithreading;
public class Join implements Runnable{
@Override
public void run() {
for (int i = 0; i < 300; i++) {
System.out.println("叼毛来了"+i++);
}
}
public static void main(String[] args) throws InterruptedException {
Join join = new Join();
Thread thread = new Thread(join);
thread.start();
for (int i = 0; i < 500; i++) {
System.out.println("正常排队"+i);
if(i==150){
thread.join();
}
}
}
}
可以看出,刚开始两线程一起执行,都是当main里的i到150时,thread就开始join,接下来其他线程阻塞,只执行thread