1. 程式人生 > >Java執行緒狀態切換詳解

Java執行緒狀態切換詳解

一、執行緒狀態說明

java.lang.Thread類中定義了執行緒狀態列舉java.lang.Thread.State,以下為各狀態說明。

1、NEW(新建)

        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

NEW是執行緒呼叫new()建立後且未呼叫start()啟動時的狀態。

2、RUNNABLE(可執行)

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

RUNNABLE包含Ready(就緒)和Running(執行中)。

就緒狀態的執行緒在系統排程分配時間片後進入執行中。

Thread.yield()呼叫後執行緒從執行中進入到就緒狀態,但是不會使用同步。

3、BLOCKED(阻塞)

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

BLOCKED是執行緒等待獲取監視器鎖去進入(或重新進入)同步程式碼塊或者同步方法。

4、WAITING(等待)

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

當呼叫Obeject.wait()、Thread.join()、LockSupport.park()方法後執行緒進入WAITING。

WAITING是執行緒無限期等待,等待其他執行緒執行特定操作:

(1)、當執行緒呼叫Object.wait()後,需要等待其他執行緒呼叫Object.notify()或者Object.notifyAll()喚醒;

(2)、當其他執行緒呼叫Thread.join()後,需要等他其他執行緒執行結束後喚醒;

5、TIMED_WAITING(超時等待)

        /**
         * Thread state for a waiting thread with a specified waiting time.
         * A thread is in the timed waiting state due to calling one of
         * the following methods with a specified positive waiting time:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

TIMED_WAITING是指執行緒等待特定時間,超時後進入RUNNABLE狀態。

呼叫以下方法會使執行緒進入TIMED_WAITING:

(1)、Thread.sleep(long millis);

執行緒sleep,不會釋放同步鎖。

(2)、Object.wait(long timeout);

等待Object.notify()或者Object.notifyAll()後或者超時後繼續執行;

必須在同步塊和同步方法中使用,呼叫前必須持有鎖,呼叫後會釋放同步鎖。

(3)、thread.join(long millis);

等待thread執行緒執行結束後活超時後繼續執行。

實際使用wait()實現。

(4)、LockSupport.parkNanos(long nanos);

在阻塞呼叫執行緒nanos納秒後繼續執行,無需持有鎖。

(5)、LockSupport.parkUntil(long deadline);

阻塞當前執行緒至deadline時間戳後或LockSupport.unpack(thread)繼續執行,無需持有鎖。deadline:1970.1.1起的毫秒數。

6、TERMINATED(結束)

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;

TERMINATED是指執行緒完成執行。

二、執行緒狀態切換

後面將根據jdk原始碼瞭解這些操作執行緒的具體方法的實現。

參考: