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

加锁堵塞状态


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

相关推荐
点心的游戏开发世界15 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
phltxy15 小时前
C语言操作符详解
java·c语言·算法
步行cgn16 小时前
@Configuration 详解:Spring 配置类的核心注解
java·后端·spring
sunshine22 girl16 小时前
Java学习一 环境配置2 安装和基本使用Idea
java·学习·intellij-idea
事圆则缓17 小时前
Kotlin 协程完全指南
开发语言·kotlin
我爱写代码i17 小时前
BeLink - 支持生成多种URL 缩短网址PHP源码
开发语言·php·短网址php源码
Java后端的Ai之路17 小时前
20、Python - 备忘录模式
开发语言·人工智能·python·外观模式·备忘录模式
嘿嘿-6618 小时前
Windows 一键使用 GPT-6 Astra:Codex CLI 配置教程
java·人工智能·windows·gpt·chatgpt·web
统计学小王子18 小时前
数学建模国赛倒计时4天——《统计模型精讲(混合效应模型R语言实现)》
开发语言·数学建模·r语言
知无不研18 小时前
c语言中循环的介绍与简单应用
c语言·开发语言·算法·循环·for·while