多线程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

加锁堵塞状态


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

相关推荐
蜡笔小谢2 小时前
DeepSeek Harness Java 实战笔记(五)|插件、MCP 与 Skill
java·笔记·ai·agent·deepseek·harness
FRing032 小时前
vue2干货总结12(loading效果、全局前置导航守卫、搜索历史管理)
开发语言·前端·javascript·vue.js·vue2
wefg12 小时前
【项目】C++ 基于 Vibe Coding 的 OJ 平台
开发语言·c++
唐老板3 小时前
Spring AI 2.0 只支持 Boot 4
java
琳少_6 小时前
java程序设计笔记
java·开发语言·数据库·笔记
小荷才露尖尖角,早有蜻蜓立上头7 小时前
SpringBoot 定时任务
java·spring boot·后端
Helix2508 小时前
JDK高频报错速查手册:javac不是内部命令、内存溢出、版本不兼容等12类问题解决方法
java·开发语言·内存溢出·java异常处理·jdk版本兼容·jdk报错·javac报错
郝学胜-神的一滴9 小时前
Effective STL 条款 07:指针容器销毁前先删除
开发语言·c++·vscode·leetcode·rpc·visual studio
CoderYanger9 小时前
A.每日一题:2243. 计算字符串的数字和
java·开发语言·程序人生·leetcode·面试·蓝桥杯·学习方法
摇滚侠10 小时前
《On Java 中文版 基础卷》阅读笔记 接口 19
java·开发语言·笔记