狀態圖
六種狀態之一
先來一波官方的解釋
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.
退出
六種狀態之二
先看圖的左側.是線程狀態的主軸線.
再來看右側
BLOCKD
和RUNNABLE
之間的轉換
WAITING
和RUNNABLE
之間的轉換
-TIME_WAITING
和RUNNABLE
之間的轉換
如何記憶
- 左邊三個是
new
runnable
terminated
; - 右邊三個是
block
time_waiting
waiting
; - 右邊三個和
runnable
之間的關係密切; - block是因爲synchronized關鍵字