java多线程基础:线程的状态

本文详细介绍了Java中线程的六种状态,包括初始状态、运行状态、堵塞状态、等待状态、超时等待状态和终止状态,并解释了每种状态的具体含义及应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开jdk源码,在Thread类中找到State枚举类。

 /**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    public enum State {
      
        NEW,

   
        RUNNABLE,

   
        BLOCKED,

    
        WAITING,


        TIMED_WAITING,

    
        TERMINATED;
    }

一共有6个状态。

NEW:初始状态,线程被构造,还未执行start()方法。(在构造方法中是这个状态)

RUNNABLE:运行状态,java 中把就绪和运行两种状态都成为运行中。(正常运行中是这个状态)

BLOCKED:堵塞状态,表示线程堵塞于锁.。(等待锁是这个状态)

WAITTING:等待状态,表示线程进入等待需要其他线程唤醒(调用wait后是这个状态)

TIME_WAITTING:超时等待状态,不同于WAITTING,到达指定时间,自动返回。(wait(long)是这个状态)

TERMINATED:终止状态,代表当前线程执行结束。(线程结束)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值