目標執行緒的join方法暫停當前執行緒,直到目前執行緒完成(從run()方法返回)。
Java程式碼:
package Threads; import java.io.IOException; /** * Created by Frank */ public class Join { public static void main(String[] args) { Thread t = new Thread() { public void run() { System.out.println("Reading"); try { System.in.read(); } catch (IOException e) { System.err.println(e); } System.out.println("Thread finished."); } }; System.out.println("Starting"); t.start(); System.out.println("Joining"); try { t.join(); } catch (InterruptedException e) { // 不應該發生 System.err.println("Who dares interrupt my sleep??"); } System.err.println("Main Finished"); } }