当前位置: 首页 > article >正文

Java EE——线程状态

前言

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

六种划分

在这里插入图片描述
调用getState()方法获取当前线程状态

一.NEW

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

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

二.RUNNABLE

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

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)

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

定义:线程处于等待状态

  1.  等待其他线程执行完毕(join)
    
  2.  其他线程来唤醒(notify)
    
  3.  等待一定的时间后自动唤醒
    
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(锁竞争)状态

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

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

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一样,线程执行完毕或者被强制终止


http://www.kler.cn/a/612933.html

相关文章:

  • 手机抓取崩溃的log日志(安卓/ios)
  • C笔记20250325
  • 位运算算法:解锁高效编程的钥匙
  • 【C#】`Interlocked` vs `lock`
  • debug 笔记:llama 3.2 部署bug 之cutlassF: no kernel found to launch!
  • 歌词json
  • 【redis】持久化之RDB与AOF
  • [c语言日寄]柔性数组
  • [Java微服务架构]7-3_事务处理——分布式事务
  • UML之包含用例
  • 基于 Qt / HTTP/JSON 的智能天气预报系统测试报告
  • 智慧电力:点亮未来能源世界的钥匙
  • 推荐:大模型靠啥理解文字?通俗解释:词嵌入embedding
  • 网络安全法律法规简介
  • RFID技术在机器人中的核心应用场景及技术实现
  • AI PPT哪家强?2025年4款高效工具深度测评
  • 【数据分享】基于联合国城市化程度框架的全球城市边界数据集(免费获取/Shp格式)
  • 对匿名认证的理解
  • [Java微服务架构]7-2_事务处理——全局事务与共享事务
  • Python每日一题(7)