Java Thread的6中狀態圖

狀態圖

Thread Map

六種狀態之一

先來一波官方的解釋
A thread state. A thread can be in one of the following states:

  • NEW

A thread that has not yet started is in this state.
創建了但是沒有start

  • RUNNABLE

A thread executing in the Java virtual machine is in this state.
可運行的(不是正在運行)

  • BLOCKED

A thread that is blocked waiting for a monitor lock is in this state.
被阻塞等到鎖(被synchronized 修飾)

  • WAITING

A thread that is waiting indefinitely for another thread to perform a particular action is in this state.
等待另外一個線程的消息.

  • TIMED_WAITING

A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
時間等待

  • TERMINATED

A thread that has exited is in this state.
退出

六種狀態之二

先看圖的左側.是線程狀態的主軸線.

Created with Raphaël 2.2.0NEWRUNNABLETERMINATED

再來看右側

Created with Raphaël 2.2.0BLOCKEDWAITINGTIME_WAITING
  • BLOCKDRUNNABLE之間的轉換
獲取了monitor鎖
進入到被synchronized修飾的方法或者代碼塊
BLOCKED
RUNNABLE
  • WAITINGRUNNABLE之間的轉換
Object.wait
Thread.join
LockSuper.park
Object.notify
Object.notifyAll
LockSuper.unpark
RUNNABLE
WAITING

-TIME_WAITINGRUNNABLE之間的轉換

Thread.sleep
Object.wait
Thread.join
Waiting time coming
Object.notify
Object.notifyAll
RUNNABLE
TIME_WAITING

如何記憶

  • 左邊三個是new runnable terminated;
  • 右邊三個是block time_waiting waiting;
  • 右邊三個和runnable之間的關係密切;
  • block是因爲synchronized關鍵字
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章