Thread.join
当前线程可以调用另一个线程的join方法,调用后当前线程会被阻塞不再执行,直到被调用的线程执行完毕,当前线程才会执行
package gov.mof.fasp2.gcfr.adjustoffset.adjust; public class Main { public static void main(String[] args) throws Exception { Runnable r1 = new Processor(); Thread t1 = new Thread(r1, "t1"); t1.start(); try { t1.join();//主线程阻塞,等待t1线程执行结束才能继续执行主线程 }catch(InterruptedException e) { e.printStackTrace(); } System.out.println("------main end-------"); } } class Processor implements Runnable { public void run() { for (int i=0; i<10; i++) { try { Thread.sleep(1000); System.out.println(Thread.currentThread().getName() + "," + i); } catch (InterruptedException e) { e.printStackTrace(); } } } }
运行结果: