本文介绍了Java线程状态转换,WAITING到BLOCKED还是RUNNABLE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 after notify() or notifyAll() 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 to BLOCKED after invoking notify() or notifyAll(); diagram below illustrates this transition in green.

    Question

    Why do most state diagrams on the web illustrate the transition from WAITING to RUNNABLE, not BLOCKED? 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 a notify (or even from a spurious wakeup) it needs to relock the monitor of the object on which it was waiting. This is the BLOCKED state.

    This is explained in the javadoc of Object#notify():

    and Object#wait()

    这篇关于Java线程状态转换,WAITING到BLOCKED还是RUNNABLE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 06:43