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

加锁堵塞状态


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

相关推荐
Le_ee1 分钟前
ctfweb:php/php短标签/.haccess+图片马/XXE
开发语言·前端·php
Zephyr_015 分钟前
Leedcode算法题
java·算法
苍煜40 分钟前
Java开发IO零基础吃透:BIO、NIO、同步异步、阻塞非阻塞
java·python·nio
yong99901 小时前
MATLAB读取高光谱图像
开发语言·matlab
2zcode1 小时前
基于MATLAB的肝病风险评估与分期分析系统设计与实现
开发语言·matlab
小小de风呀1 小时前
de风——【从零开始学C++】(五):内存管理
开发语言·c++
ooseabiscuit1 小时前
Laravel6.x核心优化与特性全解析
android·开发语言·javascript
折哥的程序人生 · 物流技术专研1 小时前
Java面试85题图解版(一):基础核心篇
java·开发语言·后端·面试
AllData公司负责人1 小时前
通过Postgresql同步到Doris,全视角演示AllData数据中台核心功能效果,涵盖:数据入湖仓,数据同步,数据处理,数据服务,BI可视化驾驶舱
java·大数据·数据库·数据仓库·人工智能·python·postgresql
Hello.Reader2 小时前
算法基础(十)——分治思想把大问题拆成小问题
java·开发语言·算法