线程有几个状态

由源码可知:6个

	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;
    }
### Java 线程状态种类及其特点 Java 中的线程状态由 `Thread.State` 枚举定义,共有六种主要状态。以下是每种状态的具体描述: #### 1. 新建状态 (NEW) 当一个线程被创建但尚未启动时,它处于新建状态。此时,线程仅作为一个对象存在于堆内存中,还未与底层操作系统的线程资源绑定[^3]。 ```java private void testStateNew() { Thread thread = new Thread(() -> {}); System.out.println(thread.getState()); // 输出 NEW } ``` --- #### 2. 可运行状态 (RUNNABLE) 当线程调用了 `start()` 方法后,线程进入可运行状态。这表示该线程已经准备好执行,但它不一定正在占用 CPU 资源。只有当线程调度器分配给它 CPU 时间片时,才会真正开始运行其逻辑[^2]。 - **特点**: - 此状态下,线程可能正实际运行,也可能因调度原因暂时未获得 CPU 执行权。 - 如果当前线程的时间片耗尽或者显式调用 `yield()` 方法,则会重新回到就绪队列等待下一次调度[^4]。 --- #### 3. 阻塞状态 (BLOCKED) 阻塞状态发生在多线程竞争同步锁的情况下。如果一个线程试图获取已被其他线程持有的锁,则该线程会被置于阻塞状态直到锁释放。 - **特点**: - 处于 BLOCKED 状态线程不会消耗任何 CPU 资源。 - 当锁定的对象可用时,线程将返回 RUNNABLE 状态。 --- #### 4. 等待状态 (WAITING) 当线程调用无超时参数的方法(如 `Object.wait()` 或 `Thread.join()`),则进入 WAITING 状态。这种状态下的线程需要依赖外部条件触发才能继续执行。 - **特点**: - 必须通过特定事件唤醒,例如通知机制 (`notify/notifyAll`) 或目标线程完成。 --- #### 5. 带时限等待状态 (TIMED_WAITING) 带时限等待状态类似于 WAITING,不同之处在于它具有固定的等待时间。常见的方法有 `Thread.sleep(long millis)` 和 `wait(long timeout)`。 - **特点**: - 即使没有外界干预,经过指定时间段后也会自动恢复到 RUNNABLE 状态。 - 是一种更灵活的暂停方式。 --- #### 6. 终止状态 (TERMINATED) 一旦线程完成了它的任务或由于异常退出而终止,就会进入 TERMINATED 状态。此后,该线程无法再被重用或重启。 - **特点**: - 表明线程生命周期结束。 - 不允许再次调用已终止线程的 `run()` 方法。 --- ### 总结代码示例 以下是一段用于遍历并打印所有线程状态的代码片段: ```java public class ThreadStatesExample { public static void main(String[] args) { for (Thread.State state : Thread.State.values()) { System.out.println(state); } } } ``` 上述代码能够展示所有的线程状态名称列表[^1]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值