線程的幾種狀態(tài).md

NEW

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

線程已經(jīng)創(chuàng)建,但是還未啟動

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.
*/

一個正在被執(zhí)行的線程的狀態(tài)

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

一個線程因為等待臨界區(qū)(同步代碼塊或者同步方法,非java.util.concurrent庫中的鎖)的鎖被阻塞產(chǎn)生的狀態(tài),Lock 或者synchronize 關(guān)鍵字產(chǎn)生的狀態(tài)?;蛘咧剡M入臨界區(qū)的鎖當(dāng)調(diào)用wait之后

WAITTING

/**
 * 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.
 */

一個線程進入了鎖,但是需要等待其他線程執(zhí)行某些操作。時間不確定,當(dāng)wait,join,park方法調(diào)以及等待Lock或Condition用時,進入waiting狀態(tài)。前提是這個線程已經(jīng)擁有鎖了。

TIMED_WAITTING

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

一個線程進入了鎖,但是需要等待其他線程執(zhí)行某些操作。時間確定,通過sleep或wait timeout方法進入的限期等待的狀態(tài)

TERMINATED

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

線程已經(jīng)終止

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容