需要用的方法 thread.join() 将并行的线程变为串行的线程
public class ThreadDemo16 {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 20; i++) {
int number = i;
Thread thread = new Thread( () -> {
System.out.println(number);
} );
thread.start();
thread.join();
}
System.out.println("ok");
}
}
运行结果: