保证线程执行顺序:
原题目:
public static void main(String[] args) { System.out.println("start"); new Thread(()-> System.out.println(1)).start(); System.out.println(2); new Thread(()-> System.out.println(3)).start(); System.out.println("end"); }
改写上述代码以 start,1,2,3,end 的顺序执行
可以采用 join的方式:
public static void main(String[] args) throws InterruptedException { System.out.println("start"); Thread t1=new Thread(()-> System.out.println(1)); Thread t2=new Thread(()-> System.out.println(3)); t1.start(); t1.join(); System.out.println(2); t2.start(); t2.join(); System.out.println("end"); }