问题描述
SO共识与互联网上几乎所有Java线程状态图之间似乎存在差异;具体来说,关于 notify()
或<$ c $之后来自 WAITING
的线程状态转换 c> notifyAll()被调用...
- 等待 state。
这在:
和
There seems to be a discrepancy between SO consensus and nearly every Java thread state diagram on the Internet; specifically, regarding thread state transition from
WAITING
afternotify()
ornotifyAll()
is invoked...- WAITING never goes directly to RUNNABLE
- The thread is WAITING until it is notified...Then it becomes BLOCKED...
- Once this thread is notified, it will not be runnable...This is..Blocked State.
So the concensus on SO is: a thread transitions from
WAITING
toBLOCKED
after invokingnotify()
ornotifyAll()
; diagram below illustrates this transition in green.Question
Why do most state diagrams on the web illustrate the transition from
WAITING
toRUNNABLE
, notBLOCKED
? Depiction in red shows the incorrect transition; am I missing something?解决方案Any diagram that shows a
notify
invocation bringing a thread from WAITING to RUNNABLE is wrong (or is using an unclarified shortcut). Once a thread gets awoken from anotify
(or even from a spurious wakeup) it needs to relock the monitor of the object on which it was waiting. This is theBLOCKED
state.This is explained in the javadoc of
Object#notify()
:and
Object#wait()
这篇关于Java线程状态转换,WAITING到BLOCKED还是RUNNABLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!