線程的狀態

Thread.State

首先看JDK中的代碼: java.lang.Thread.Statejava

/**
     * A thread state.  A thread can be in one of the following states:
     * 一個線程的狀態,一個線程能夠處於如下狀態中的某一個狀態
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,
​
        /**
         * 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,
​
        /**
         * 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,
​
        /**
         * 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,
​
        /**
         * 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,
​
        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

一、新建(New)

新建立了一個線程對象,還未調用start()方法。this

二、就緒(Runnable)

線程對象建立後,其餘線程(好比main線程)調用了該對象的start()方法。該狀態的線程位於可運行線程池中,等待被線程調度選中 獲取cpu 的使用權 。線程

三、運行中(Running,線程狀態中並無這一狀態,可是實際執行中是有的)

可運行狀態(runnable)的線程得到了cpu 時間片(timeslice) ,執行程序代碼。code

四、限期等待(Timed Waiting)

也能夠稱做 TIMED_WAITING(有等待時間的等待狀態)。orm

線程主動調用如下方法:對象

Thread.sleep方法;生命週期

Object的wait方法,帶有時間;ci

Thread.join方法,帶有時間;get

LockSupport的parkNanos方法,帶有時間。同步

五、無限期等待(Waiting)

運行中(Running)的線程執行了如下方法:

Object的wait方法,而且沒有使用timeout參數;

Thread的join方法,沒有使用timeout參數;

LockSupport的park方法;

Conditon的await方法。

六、阻塞(Blocked)

阻塞狀態是指線程由於某種緣由放棄了cpu 使用權,暫時中止運行。直到線程進入可運行(runnable)狀態,纔有機會再次得到cpu timeslice 轉到運行(running)狀態。阻塞的狀況分兩種:

同步阻塞:運行(running)的線程進入了一個synchronized方法,若該同步鎖被別的線程佔用,則JVM會把該線程放入鎖池(lock pool)中。

其餘阻塞:運行(running)的線程發出了I/O請求時,JVM會把該線程置爲阻塞狀態。當I/O處理完畢時,線程從新轉入可運行(runnable)狀態。

七、結束(Terminated)

線程run()、main() 方法執行結束,或者因異常退出了run()方法,則該線程結束生命週期。

線程狀態探祕

jstack查看線程狀態
jstack -l <pid> 便可察看線程狀態,如何使用呢?

隨便寫一個死循環看一下

public class TestThreadState {
    public static void main(String[] args) {
        for (; ; ) {
​
        }
    }
}

ps -ef|grep TestThreadState,找到對應的pid,jstack -l <pid>便可,若是未輸出線程信息,能夠嘗試使用-F參數來強制輸出。

"main" #1 prio=5 os_prio=31 tid=0x00007f8194801800 nid=0x1603 runnable [0x000070000a9b4000]
   java.lang.Thread.State: RUNNABLE
        at org.java.bin.TestThreadState.main(TestThreadState.java:12)
相關文章
相關標籤/搜索