深入体会线程状态的切换
创始人
2024-03-17 17:00:56
0

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

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

在这里插入图片描述

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

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

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

    • NEW:
    • RUNNABLE:
    • WAITING:
    • TIMED_WAITING:
    • BLOCKED:
    • TERMINATED:

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());}
}

在这里插入图片描述

在构建线程后没有启动线程前,线程处于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);}
}

在这里插入图片描述

在此时,线程已经启动所以状态为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());}
}

在这里插入图片描述

在这里插入图片描述

我们使用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());}
}

在这里插入图片描述

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

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 都要争夺执行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());}
}

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

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

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...