join示例分析
join示例分析
public class TestJoin { public static void main(String[] args) throws InterruptedException { System.err.println(Thread.currentThread().getName()+"...start"); JoinThread t1 = new JoinThread("t1"); t1.start(); System.err.println(Thread.currentThread().getName()+"...t1.join... wait t1 died."); t1.join();//让t1加入到当前线程main,然后main线程进入阻塞状态,直到t1执行run完成 System.err.println(Thread.currentThread().getName()+"....end"); } static class JoinThread extends Thread { public JoinThread(String name) { super(name); } @Override public void run() { System.err.println(Thread.currentThread().getName()+"....run start"); for (int i = 0; i < 5; i++) { try { System.err.println(Thread.currentThread().getName()+"..."+i); Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } System.err.println(Thread.currentThread().getName()+"........run end"); } } }