Java的執行緒有6個狀態,分別是
NEW 新建狀態。剛new出來的thread,還沒有呼叫start,就是這個狀態
RUNNABLE 執行狀態(分為執行中和就緒)。正在執行,或者等待CPU排程,呼叫完start,就是這個狀態
BLOCKED 阻塞狀態。還未競爭到的鎖的阻塞在那的情況,使用synchronize,但是還沒競爭到鎖
WAITING 等待狀態。使用wait、join、park都會進入這個狀態
TIMED_WAITING 計時等待狀態。使用sleep、wait(時間)、join(時間)、parkNanos、parkUntil都會進入這個狀態
TERMINATED 終止狀態。正常執行完run方法以後
如下是各個狀態的轉換圖
可以從原始碼看到這些列舉值
如下是為了加深理解,模擬出的執行緒的各個狀態
public class ThreadStateTest { public static void main(String[] args) throws InterruptedException { NEW_RUNNABLE_TERMINATED(); BLOCKED_by_synchronized(); WAITING_by_wait(); WAITING_by_join(); WAITING_by_park(); TIMED_WAITING_by_wait_timeout(); TIMED_WAITING_by_sleep(); TIMED_WAITING_by_join_timeout(); TIMED_WAITING_by_parkUntil(); TIMED_WAITING_by_parkNanos(); } private static void NEW_RUNNABLE_TERMINATED() throws InterruptedException { Thread t1 = new Thread(() -> { while(!Thread.currentThread().isInterrupted()) { for(long j=0;j<100;j++) {} } }); System.out.println("state:" + t1.getState() + " after new"); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " after start"); t1.interrupt(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " after all"); } private static void BLOCKED_by_synchronized() throws InterruptedException { Object o1 = new Object(); Thread t1 = new Thread(() -> { synchronized (o1) { } }); synchronized (o1) { t1.start(); Thread.sleep(1000); System.out.println("state:" + t1.getState() + " BLOCKED_by_synchronized"); } } private static void WAITING_by_wait() throws InterruptedException { Object o1 = new Object(); Thread t1 = new Thread(() -> { synchronized (o1) { try { o1.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } }); t1.start(); Thread.sleep(1000); System.out.println("state:" + t1.getState() + " WAITING_by_wait"); synchronized (o1) { o1.notifyAll(); } } private static void TIMED_WAITING_by_wait_timeout() throws InterruptedException { Object o1 = new Object(); Thread t1 = new Thread(() -> { synchronized (o1) { try { o1.wait(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_wait_timeout"); } private static void WAITING_by_join() throws InterruptedException { Thread t1 = new Thread(() -> { try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } }); Thread t2 = new Thread(() -> { t1.start(); try { t1.join(); } catch (InterruptedException e) { throw new RuntimeException(e); } }); t2.start(); Thread.sleep(500); System.out.println("state:" + t2.getState() + " WAITING_by_join"); } private static void WAITING_by_park() throws InterruptedException { Thread t1 = new Thread(() -> { LockSupport.park(); }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " WAITING_by_park"); Thread.sleep(1000); LockSupport.unpark(t1); } private static void TIMED_WAITING_by_sleep() throws InterruptedException { Thread t1 = new Thread(() -> { try { Thread.sleep(2 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_sleep"); } private static void TIMED_WAITING_by_join_timeout() throws InterruptedException { Thread t1 = new Thread(() -> { try { Thread.sleep(4 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } }); Thread t2 = new Thread(() -> { t1.start(); try { t1.join(2 * 1000); } catch (InterruptedException e) { throw new RuntimeException(e); } }); t2.start(); Thread.sleep(500); System.out.println("state:" + t2.getState() + " TIMED_WAITING_by_join_timeout"); } private static void TIMED_WAITING_by_parkUntil() throws InterruptedException { Thread t1 = new Thread(() -> { long nowMills = System.currentTimeMillis(); LockSupport.parkUntil(2*1000+nowMills); }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_parkUntil"); } private static void TIMED_WAITING_by_parkNanos() throws InterruptedException { Thread t1 = new Thread(() -> { LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(2)); }); t1.start(); Thread.sleep(500); System.out.println("state:" + t1.getState() + " TIMED_WAITING_by_parkNanos"); } }