• 深入体会线程状态的切换


    ✨✨hello,愿意点进来的小伙伴们,你们好呐!
    🐻🐻系列专栏:【JavaEE初阶】
    🐲🐲本篇内容:线程状态详解
    🐯🐯作者简介:一名现大二的三非编程小白,日复一日,仍需努力。

    Java线程的运行的生命周期可以分为6种不同的状态,在某一时刻线程只能处于其中一种状态

    在这里插入图片描述

    NEW : 初始状态,表示线程任务被构建,但是还没有调用start()方法.
    RUNNABLE : 运行状态,在这个状态中,线程正在执行中或者线程在就绪队列中等待CPU的调度都为这个状态.
    BLOCKED : 阻塞状态,表示线程阻塞干预.
    WAITING : 等待状态,表示线程当前正在等待其他线程,比如调用 join(),wait()方法都会导致线程进入该状态
    TIME_WAITING : 超时等待状态,这个状态不同意WAITING,这个状态也是等待,但是它会等待指定的时间后自己返回.
    TERMINATED : 终止状态,表示当前线程已经执行完毕,即PCB释放了,但是线程对象的引用还在.

    下面的图解会让我们更清晰的了解到线程创建,执行,等待,通知,销毁的一系列流程中的状态
    在这里插入图片描述

    💦💦💦接下来我来带大家体会线程的状态

    NEW:

    public class ThreadDemo_NEW {
        public static void main(String[] args) {
            Thread thread = new Thread(() -> {
                System.out.println("演示NEW");
            });
            System.out.println(thread.getName() + " 状态为: " + thread.getState());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    在构建线程后没有启动线程前,线程处于NEW状态

    RUNNABLE:

    💨💨💨因为就绪状态与执行状态在Java中无法体现出来,都归为RUNNABLE状态,所以我就来展示RUNNABLE状态

    public class ThreadDemo_RUNNABLE {
        public static void main(String[] args) throws InterruptedException {
            Thread thread = new Thread(() -> {
                System.out.println("演示RUNNABLE");
            });
    
            thread.start();
            System.out.println(thread.getName() + " 状态为: " + thread.getState());
            Thread.sleep(1000);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述

    在此时,线程已经启动所以状态为RUNNABLE

    WAITING:

    public class ThreadDemo_WAITING {
        public static void main(String[] args) throws InterruptedException {
            Object o = new Object();
            Thread thread = new Thread(() -> {
                System.out.println("演示WAITING");
                synchronized (o){
                    try {
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
            });
            thread.start();
            Thread.sleep(1000);
            System.out.println(thread.getName() + " 状态为: " + thread.getState());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    在这里插入图片描述

    我们使用wait() 方法让Thread-0线程进入等待状态,这个时候没有指定等待时间,所以是死等

    TIMED_WAITING:

    public class ThreadDemo_TIMED_WAITING {
        public static void main(String[] args) throws InterruptedException {
            Object o = new Object();
            Thread thread = new Thread(() -> {
                System.out.println("演示TIMED_WAITING");
                synchronized (o){
                    try {
                        o.wait(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
            });
            thread.start();
            Thread.sleep(1000);
            System.out.println(thread.getName() + " 状态为: " + thread.getState());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    当指定等待时间后,线程就进入了超时等待的状态

    BLOCKED:

    public class ThreadDemo_BLOCKED {
        public static void main(String[] args) throws Exception {
            testBlocked();
        }
        public static void testBlocked() throws Exception {
            class Counter {
                int counter;
                public synchronized void increase() {
                    counter++;
                    try {
                        Thread.sleep(30000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
    
            Counter c = new Counter();
    
            Thread t1 = new Thread(new Runnable() {
                public void run() {
                    c.increase();
                }
            }, "t1线程");
            t1.start();
    
            Thread t2 = new Thread(new Runnable() {
                public void run() {
                    c.increase();
                }
            }, "t2线程");
            t2.start();
    
            Thread.sleep(100); // 确保 t2 run已经得到执行
            System.out.println(t2.getName() + " 状态为: " + t2.getState());
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    线程1 与线程2 都要争夺执行increase() 方法,最终导致的线程2阻塞

    TERMINATED:

    public class ThreadDemo_TERMINATED {
        public static void main(String[] args) throws InterruptedException {
            Thread thread = new Thread(() -> {
                System.out.println("演示TERMINATED");
            });
            thread.start();
            Thread.sleep(1000);
            System.out.println(thread.getName() + " 状态为: " + thread.getState());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    线程执行结束后,但是线程引用还有指向,未销毁,所以这个时候线程的状态就是终止状态

    通过上面的展示,我们可以了解到线程自身的生命周期并不是固定的,而是通过代码执行在不同的状态下进行切换,对于线程的状态我们要很清楚的把握住.

  • 相关阅读:
    Hadoop:Hive操作(二):数据表操作,复杂数据类型,Sampling采样,虚拟列
    数据监测全过程——采集、清洗、分析
    C++保留小数点后两位(floor&ceil&round)详解
    使用js实现安居客二级菜单及(注意事项和问题点演示)完整版
    Java修仙之高级功法篇->MybatisPlus
    基于用户行为的交易反欺诈探索
    Windows 11 企业版新功能介绍
    图片划过缩放
    自己动手实现一个深度学习算法——三、神经网络的学习
    这份Github神仙笔记覆盖了90%以上的Java八股文
  • 原文地址:https://blog.csdn.net/m0_62547912/article/details/128173514