Java EE——线程状态

前言

从编写Java代码的角度来说,线程一共有六种状态;但是以操作系统的视角来看,线程状态可以分为物种

六种划分

调用getState()方法获取当前线程状态

一.NEW

定义:线程(对象)被创建但还没有启动

java 复制代码
public class NEW {
    public static void main(String[] args) {
        Thread thread = new Thread(()->{});
        //thread创建完毕
        //NEW
        System.out.println(thread.getState());
    }
}

二.RUNNABLE

定义:线程创建并启动完毕,拥有执行代码的能力或者正在执行代码

java 复制代码
public class RUNNABLE {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            while (true){
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("Hello Thread");
            }
        });
        //调用start
        thread.start();
        //RUNNABLE
        System.out.println(thread.getState());
    }
}

三.WAITING

定义:线程处于等待状态,等待其他线程执行完毕(join)或者其他线程来唤醒(notify)

java 复制代码
public class WAITING {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Thread thread = new Thread(()->{
            synchronized (locker){
                try {
                    //无时间参数的wait
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        Thread.sleep(100);
        //WAITING
        System.out.println(thread.getState());
    }
}

四.TIMED_WAITING

定义:线程处于等待状态

复制代码
    等待其他线程执行完毕(join)
复制代码
    其他线程来唤醒(notify)
复制代码
    等待一定的时间后自动唤醒
java 复制代码
public class TIMED_WAITING {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Thread thread = new Thread(()->{
            synchronized (locker){
                try {
                    //有时间参数的wait
                    locker.wait(10000000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread.start();
        Thread.sleep(100);
        //
        System.out.println(thread.getState());
    }
}

五.BLOCKED

定义:线程竞争锁对象失败进入BLOCKED(锁竞争)状态

java 复制代码
public class BLOCKED {
    public static void main(String[] args) throws InterruptedException {
        Object locker = new Object();
        Thread thread1 = new Thread(()->{
            synchronized (locker){
                try {
                    Thread.sleep(1000000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        thread1.start();
        //thread1先拿到locker锁对象
        Thread thread2 = new Thread(()->{
            while (true) {
                synchronized (locker) {
                    System.out.println("Hello Thread2");
                }
            }
        });
        thread2.start();
        //sleep(1000)的作用是让thread2有足够用的时间执行到synchronized
        Thread.sleep(100);
        //获取线程状态
        //BLOCKED
        System.out.println(thread2.getState());
    }
}

六.TERMINATED

定义:线程执行完毕或者因异常退出

java 复制代码
public class TERMINATED {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{});
        thread.start();
        //等待线程执行完毕
        thread.join();
        //TERMINATED
        System.out.println(thread.getState());
    }
}

五种划分

一.新建

和NEW一样

二.就绪

CPU已经为线程分配好了时间,但还没来得及执行代码

三.运行

CPU已经为线程分配好了时间,并且正在执行代码

四.阻塞

线程启动完毕,但被暂停执行(可能是自身原因,也可能是外部原因),有以下几种情况

1.等待其他线程释放锁对象

2.等待文件IO,如

3.调用wait(无参数/有参数)方法

五.终止

和TERMINATED一样,线程执行完毕或者被强制终止

相关推荐
stillaliveQEJ17 分钟前
【javaEE】Spring AOP(一)
java·spring·java-ee
麦兜*18 分钟前
SpringBoot进阶:深入理解SpringBoot自动配置原理与源码解析
java·spring boot·spring·spring cloud
慕白Lee19 分钟前
项目JDK17+SpringBoot3.0升级
java·ide·intellij-idea
佛系打工仔6 小时前
绘制K线第二章:背景网格绘制
android·前端·架构
之歆7 小时前
Spring AI入门到实战到原理源码-MCP
java·人工智能·spring
yangminlei8 小时前
Spring Boot3集成LiteFlow!轻松实现业务流程编排
java·spring boot·后端
qq_318121598 小时前
互联网大厂Java面试故事:从Spring Boot到微服务架构的技术挑战与解答
java·spring boot·redis·spring cloud·微服务·面试·内容社区
J_liaty8 小时前
Spring Boot整合Nacos:从入门到精通
java·spring boot·后端·nacos
阿蒙Amon9 小时前
C#每日面试题-Array和ArrayList的区别
java·开发语言·c#
daidaidaiyu9 小时前
Spring IOC 源码学习 一文学习完整的加载流程
java·spring