thread.join()主线程需要等待子线程执行完成之后再结束
public class TestDemo { public static void main(String[] args) { try { Test.calc(); } catch (Exception e) { e.printStackTrace(); } } } class Test{ private long count=0; public long add10K(String id ){ int i=0; while (i++<1000000){ count+=1; } System.out.println(id+" count:"+count); return count; } public static long calc() throws Exception{ Test test = new Test(); Thread thread1 = new Thread(()->{ test.add10K("1"); }); Thread thread2 = new Thread(()->{ test.add10K("2"); }); thread1.start(); System.out.println("1"); thread2.start(); System.out.println("2"); thread1.join(); System.out.println("3"); thread2.join(); System.out.println("4"); return 1; } }