Java Thread的6中状态图

    技术2026-01-11  11

    文章目录

    状态图六种状态之一六种状态之二如何记忆

    状态图

    六种状态之一

    先来一波官方的解释 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.0 NEW RUNNABLE TERMINATED

    再来看右侧

    Created with Raphaël 2.2.0 BLOCKED WAITING TIME_WAITING BLOCKD 和RUNNABLE之间的转换 获取了monitor锁 进入到被synchronized修饰的方法或者代码块 BLOCKED RUNNABLE WAITING 和RUNNABLE之间的转换 Object.wait Thread.join LockSuper.park Object.notify Object.notifyAll LockSuper.unpark RUNNABLE WAITING

    -TIME_WAITING 和RUNNABLE之间的转换

    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关键字
    Processed: 0.037, SQL: 9