线程究竟是五种状态还是六种状态?

858 阅读3分钟

先说结论:对于操作系统而言,线程状态是 5 种,对于 Java 而言,线程状态是 6 种。

操作系统线程状态

blog.jpg

《计算机操作系统(第四版)》 -- 汤小风版

  1. New(创建状态)
    • 当线程对象被创建但尚未启动时,线程处于新建状态。
    • 在这个阶段,操作系统正在为线程分配必要的资源,并进行初始化设置。
  2. Ready(就绪状态)
    • 在线程被创建并准备好执行时,它进入就绪状态。
    • 就绪状态表示线程已经准备好运行,等待被分配到CPU时间片进行执行。
    • 在多线程环境中,多个就绪状态的线程可能会竞争CPU资源。
  3. Running(执行状态)
    • 当线程被调度并获得CPU时间片时,它进入执行状态。
    • 执行状态表示线程正在执行其指定的任务或代码。
    • 在单核处理器系统中,同一时刻只能有一个线程处于执行状态。在多核处理器系统中,多个线程可以同时处于执行状态。
  4. Blocked(阻塞状态)
    • 当线程等待某些条件满足时,无法继续执行时,它进入阻塞状态。
    • 阻塞状态表示线程被阻塞,暂时停止执行,并且不会占用CPU资源。
    • 常见的阻塞原因包括等待I/O操作完成、等待锁、等待其他线程的通知等。
  5. Terminated(终止状态)
    • 当线程执行完其任务、显式终止或出现异常而无法继续执行时,它进入终止状态。
    • 在终止状态下,线程已经完成了它的生命周期,并且不再执行。
Java 线程状态

查看 Java 中的 Thread 源码可知:

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;
}
  1. NEW: 初始状态,线程被创建出来但没有被调用 start() 。
  2. RUNNABLE: 运行状态,线程被调用了 start() 等待运行的状态。
  3. BLOCKED:阻塞状态,需要等待锁释放。
  4. WAITING:等待状态,表示该线程需要等待其他线程做出一些特定动作(通知或中断),如源码所说,wait(),join() 和 LockSupport.park() 会进入 WAITING 状态。
  5. TIME_WAITING:超时等待状态,可以在指定的时间后自行返回而不是像 WAITING 那样一直等待。
  6. TERMINATED:终止状态,表示该线程已经运行完毕。

区别:Java 线程划分没有了就绪状态,多出了等待状态和超时等待状态。