Thread線程有哪幾種狀態,這篇文章帶你深入瞭解!

Thread線程有哪幾種狀態,這也是面試中很多小夥伴都會被問到的知識點。Thread線程狀態是應該掌握的基礎知識。但是很多小夥伴如果沒有總結過的話,面試過程可能會懵圈,這裏我稍作整理,希望小夥伴們可以查漏補缺。

Thread源碼中有定義State的枚舉類,State枚舉類中有明確定義線程的狀態。源碼如下。

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、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED。

我們分別對線程不同狀態進行簡單描述:

  1. NEW:尚未啓動的線程的線程狀態。
  2. RUNNABLE:可運行線程的線程狀態。線程正在運行或者正在獲取CPU時間片。
  3. BLOCKED:線程的線程狀態被阻止,正在等待監視器鎖。
  4. WAITING:等待線程的線程狀態。調用某個線程的wait()、join()、 locksupport park()處於等待狀態的線程。
  5. TIMED_WAITING:具有指定等待時間的等待線程的線程狀態。調用某個線程的具有指定正等待時間的方法sleep(long)、wait(long)、join(long)、LockSupport Parknanos()、LockSupport ParkUntil()處於指定等待時間的等待線程的線程狀態。
  6. TERMINATED:終止線程的線程狀態。

我這邊也整理了一份相關的線程狀態轉換圖以供大家參考。

以上代供參考,如有不當之處,歡迎指出!!!

更多幹貨,歡迎大家關注和聯繫我。期待和大家一起更好的交流、探討技術!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章