Java线程的状态

JDK 1.5 前线程状态

线程状态 中文名称 描述
New 新建 刚创建的线程,还未启动。
Runnable 可运行 线程可以运行,可能在等待 CPU 调度。
Blocked 阻塞 线程被阻塞,正在等待锁的释放。
Dead 终止 线程执行完成或异常终止,已进入结束状态。

jdk1.5之前

JDK 1.5 后线程状态

java.lang.Thread.State

public enum State {
    /**
     * Thread state for a thread which has not yet started.
     */
    NEW,

    /**
     * Thread state for a runnable thread.  A thread in the runnable
     * state is executing in the Java virtual machine but it may
     * be waiting for other resources from the operating system
     * such as processor.
     */
    RUNNABLE,

    /**
     * Thread state for a thread blocked waiting for a monitor lock.
     * A thread in the blocked state is waiting for a monitor lock
     * to enter a synchronized block/method or
     * reenter a synchronized block/method after calling
     * {@link Object#wait() Object.wait}.
     */
    BLOCKED,

    /**
     * Thread state for a waiting thread.
     * A thread is in the waiting state due to calling one of the
     * following methods:
     * <ul>
     *   <li>{@link Object#wait() Object.wait} with no timeout</li>
     *   <li>{@link #join() Thread.join} with no timeout</li>
     *   <li>{@link LockSupport#park() LockSupport.park}</li>
     * </ul>
     *
     * <p>A thread in the waiting state is waiting for another thread to
     * perform a particular action.
     *
     * For example, a thread that has called <tt>Object.wait()</tt>
     * on an object is waiting for another thread to call
     * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
     * that object. A thread that has called <tt>Thread.join()</tt>
     * is waiting for a specified thread to terminate.
     */
    WAITING,

    /**
     * Thread state for a waiting thread with a specified waiting time.
     * A thread is in the timed waiting state due to calling one of
     * the following methods with a specified positive waiting time:
     * <ul>
     *   <li>{@link #sleep Thread.sleep}</li>
     *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
     *   <li>{@link #join(long) Thread.join} with timeout</li>
     *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
     *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
     * </ul>
     */
    TIMED_WAITING,

    /**
     * Thread state for a terminated thread.
     * The thread has completed execution.
     */
    TERMINATED;
}
线程状态 中文名称 描述
New 新建 刚创建的线程,还未启动。
Runnable 可运行 线程可以运行,可能在等待 CPU 调度。
Blocked 阻塞 线程尝试获取锁失败,被阻塞,等待锁释放。
Waiting 等待 线程进入等待状态,等待其他线程显式唤醒,通常由 Object.wait() 引起。
Timed Waiting 计时等待 线程等待指定时间后自动唤醒,由 Thread.sleep()wait(time) 引起。
Terminated 终止 线程执行完成或异常终止,已进入结束状态。

jdk1.5之后


"如果文章对您有帮助,可以请作者喝杯咖啡吗?"

微信二维码

微信支付

支付宝二维码

支付宝


Java线程的状态
https://blog.liuzijian.com/post/6a0eb4a5-8e37-2791-5162-ecd7976803f5.html
作者
Liu Zijian
发布于
2024年11月3日
许可协议