Thread 类join方法,保证子线程执行完成后,再执行父线程,在start 后执行join有效
public class MainTest { @Test public void test4() throws InterruptedException { Thread t1 = new Thread(new Conn(), "t1")/*{//匿名内部类优先执行 @Override public void run(){ System.out.println(Thread.currentThread().getName()); } }*/; t1.start(); t1.join(); System.out.println(Thread.currentThread().getName()); } } class Conn implements Runnable { @Override public void run() {
//流range IntStream.range(0, 1000).forEach(i -> { System.out.println(Thread.currentThread().getName() + i); try { Thread.sleep(1_000); } catch (InterruptedException e) { e.printStackTrace(); } }/*System.out::println //lambda 方法引用*/); } }