JDK中解釋為 Waits for this thread to die. 等待本線程結束后,下一個線程才可以運行。
實例要求:
現在有T1、T2、T3三個線程,你怎樣保證T2在T1執行完后執行,T3在T2執行完后執行
實現代碼:
package com.st.lesson02; public class Test01 { //1.現在有T1、T2、T3三個線程,你怎樣保證T2在T1執行完后執行,T3在T2執行完后執行 public static void main(String[] args) throws InterruptedException { Thread th1 = new Thread01(); Thread th2 = new Thread02(); Thread th3 = new Thread03(); th1.start(); th1.join(); System.out.println("Thread01運行結束。。。"); th2.start(); th2.join(); System.out.println("Thread02運行結束。。。"); th3.start(); th3.join(); System.out.println("Thread03運行結束。。。"); System.out.println("------主函數-------"); } } class Thread01 extends Thread{ public void run() { System.out.println("Thread01...running..."); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } class Thread02 extends Thread{ public void run() { System.out.println("Thread02...running..."); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } class Thread03 extends Thread{ public void run() { System.out.println("Thread03...running..."); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } }
運行效果圖: