多线程2(Java线程的状态)

1.NEW

2.TERMINATED

3.RUNNABLE

4.WAITING

5.TIMED_WAITING

6.BLOCKED


1.NEW

这个状态代表的是start方法执行之前的状态

也就是线程被创建,但还未被执行

java 复制代码
    public static void main1(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while (true){
                System.out.println("hello t");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });

        System.out.println(t.getState());//在start之前->NEW

        t.start();
    }

2.TERMINATED

线程方法入口执行完毕

java 复制代码
    public static void main1(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            /*while (true){
                System.out.println("hello t");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }*/
        });

        System.out.println(t.getState());//在start之前->NEW

        t.start();

        t.join();

        System.out.println(t.getState());//t线程执行结束

    }

3.RUNNABLE

这个状态细分为两种:分别是就绪队列中的线程和正在执行的线程

java 复制代码
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
            while (true){

            }
        });
        t.start();

        Thread.sleep(10);
        System.out.println(t.getState());
    }

4.WAITING

死等,join的情况下,比如说main线程等待t线程执行的过程,那么此时的main就是在死等,main的状态就是WAITING

java 复制代码
    public static void main(String[] args) throws InterruptedException {
        Thread mainThread = Thread.currentThread();//获取main线程

        Thread t = new Thread(()->{
            while (true){
                System.out.println(mainThread.getState());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();

        t.join();//主线程等待t,此时一直在死等
    }

5.TIMED_WAITING

设置了超时时间的等待

比如说join方法添加了等待时间,那么等待的这段时间状态就是TIMED_WAITING

java 复制代码
    public static void main(String[] args) throws InterruptedException {
        Thread mainThread = Thread.currentThread();//获取main线程

        Thread t = new Thread(()->{
            while (true){
                System.out.println(mainThread.getState());
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t.start();

        t.join(5555);//主线程等待t,此时一直在死等
    }

6.BLOCKED

加锁堵塞状态


以上这几种状态作用于调试代码的阶段,用来发现问题

相关推荐
必须会一定会6 小时前
Agent Plugins 1.0实战:plugin.json、skills、mcp.json目录结构与迁移
开发语言·人工智能·ai编程
St_rive6 小时前
Page Object设计模式
java·开发语言·设计模式
wp123_16 小时前
硬件元器件笔记|IPX8 防水 Type‑C 母座安费诺 124018802112A 与 TONEVEE TY48086‑24A 分析
c语言·开发语言·笔记
wuyk5556 小时前
4.树:一对多的层次数据结构
开发语言·数据结构·stm32·单片机
风流 少年6 小时前
Spring AI 2.0:SSE
java·人工智能·spring
凤山老林7 小时前
精细化流量治理:Spring Boot 动态特性开关与灰度发布体系
java·spring boot·后端
luj_17687 小时前
桥牌思维启示:系统设计的模块化架构
c语言·开发语言·c++·经验分享·算法
caimouse9 小时前
ReactOS 图形系统分析(16):字符串对象 — STROBJ(string.c)
c语言·开发语言
Aphelios3809 小时前
一次锁内网络IO引发的Tomcat线程池“饿死”事故
java·开发语言·spring boot·elasticsearch·tomcat·网络io阻塞·线程池耗尽