一、join的用法java
package com.ljb.app.thread; /** * 線程調度join的使用 * @author LJB * @version 2015年3月6日 */ public class TempThread extends Thread{ public TempThread () {} public TempThread (String name) { super(name); } public void run () { for (int i = 0 ; i < 5 ; i++) { System.out.println(getName() + " " + i); } } /** * @param args */ public static void main(String[] args) { for (int i = 0 ; i < 10 ; i++) { if (i == 5) { TempThread tempThread = new TempThread(); TempThread tempThread2 = new TempThread(); try { tempThread.start(); tempThread.join(); tempThread2.start(); tempThread2.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println(Thread.currentThread().getName() + " " + i); } } }
二、運行結果app
main 0
main 1
main 2
main 3
main 4
Thread-0 0
Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-1 0
Thread-1 1
Thread-1 2
Thread-1 3
Thread-1 4
main 5
main 6
main 7
main 8
main 9
描述:主線程啓動運行被線程1阻塞,第一個線程啓動運行結束,第二個線程啓動阻塞主線程,運行結束,主線程啓動運行結束。spa
三、線程狀態線程
四、線程優先級code