一個線程Thread1嵌套線程Thread2,使用Thread1.join()不會在Thread2完成以後繼續執行。java
public class Test1 { private static Thread t4; static Thread t1 = new Thread(){ @Override public void run(){ System.out.println("1"); } }; static Thread t2 = new Thread(){ @Override public void run(){ try { t4 = new Thread(){ @SuppressWarnings("static-access") @Override public void run(){ try { t4.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("4"); } }; t4.start(); } catch (Exception e) { e.printStackTrace(); } System.out.println("2"); } }; public static void main(String[] args){ t2.start(); try { t2.join(); } catch (InterruptedException e) { e.printStackTrace(); } t1.start(); } }
執行結果ide
2
1
4線程
t1只會讓t2執行完畢,不會等待t2內部的t4執行完畢。code