java線程狀態

 最近在複習JAVA一些知識點,發現線程狀態部分,網上給出的千奇百怪,有云5個狀態的 ,有說7個狀態的。因此查了一下JDK,作下記錄,以備查詢。java

 java線程狀態(java.lang.Thread.State)
ide

源碼:this

 /**
     * 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;
    }

JDK API說明:spa

 

線程狀態。線程能夠處於下列狀態之一 操作系統

在給定時間點上,一個線程只能處於一種狀態。這些狀態是虛擬機狀態,它們並無反映全部操做系統線程狀態。 
線程

  • NEW
    至今還沒有啓動的線程處於這種狀態。
  • RUNNABLE
    正在 Java 虛擬機中執行的線程處於這種狀態。

可運行線程的線程狀態。處於可運行狀態的某一線程正在 Java 虛擬機中運行,但它可能正在等待操做系統中的其餘資源,好比處理器。
code

  • BLOCKED
    受阻塞並等待某個監視器鎖的線程處於這種狀態。

處於受阻塞狀態的某一線程正在等待監視器鎖,以便進入一個同步的塊/方法,或者在調用 Object.wait 以後再次進入同步的塊/方法。 orm

  • WAITING
    無限期地等待另外一個線程來執行某一特定操做的線程處於這種狀態。

某一等待線程的線程狀態。某一線程由於調用下列方法之一而處於等待狀態: 對象


處於等待狀態的線程正等待另外一個線程,以執行特定操做。 例如,已經在某一對象上調用了Object.wait() 的線程正等待另外一個線程,以便在該對象上調用Object.notify()Object.notifyAll()。已經調用了Thread.join() 的線程正在等待指定線程終止。 ci


  • TIMED_WAITING
    等待另外一個線程來執行取決於指定等待時間的操做的線程處於這種狀態。

具備指定等待時間的某一等待線程的線程狀態。某一線程由於調用如下帶有指定正等待時間的方法之一而處於定時等待狀態:




  • TERMINATED 已退出的線程處於這種狀態。
相關文章
相關標籤/搜索