首页 技术 正文
技术 2022年11月12日
0 收藏 826 点赞 4,553 浏览 3626 个字

@目录

线程状态 Thread.State

状态类型

在指定的时间点, 一个线程有且只有一种状态。 这些状态是 JVM 的状态, 他们并没有反映操作系统的状态。

定义

Thread 的状态是定义在 Thread 内部的枚举类型。

public enum State {
NEW,
RUNNABLE,
BLOCKED,
WAITING,
TIMED_WAITING,
TERMINATED;
}

在定义中, 我们知道共有 6 种类型。

说明

状态 说明
NEW 至今尚未启动的线程处于这种状态
RUNNABLE 正在 Java 虚拟机中执行的线程处于这种状态。 因为可能在等待其他的资源, 比如处理器。
BLOCKED 受阻塞并等待某个监视器锁的线程处于这种状态
WAITING 无限期地等待另一个线程来执行。某一特定操作的线程处于这种状态
TIMED_WAITING 等待另一个线程来执行。取决于指定等待时间的操作的线程处于这种状态
TERMINATED 已退出的线程处于这种状态

状态转换

借用 《Java 并发编程的艺术》图一张

从以上的图可以看出,

  1. 线程创建后未启动未 「NEW」 状态, 通过 start() 函数转换为 「RUNNABLE」状态。
  2. 「RUNNABLE」 状态通过各函数, 可以与「WAITING」、「TIMED-WAITING」、「BLOCKED」 进行双向切换。
  3. 「RUNNABLE」 状态在线程结束后转换为 「TERMINATED」 状态。

也就是说, 全部的状态是以 「RUNNABLE」 为中心的。

状态验证

「NEW」-> 「RUNNABLE」 -> 「TERMINATED」

创建一个实现 Runnable 的类

public class StateTestThread implements Runnable{
public StateTestThread() {
// 此时的线程还是 main
System.out.println(Thread.currentThread().getName()+" state in Constructor:"+
Thread.currentThread().getState());
} public void run() {
System.out.println(Thread.currentThread().getName()+" state in Run:"+
Thread.currentThread().getState());
}
}

创建一个测试类

public class ThreadStateTest {    public static void main(String[] args) {
StateTestThread stateTestThread = new StateTestThread();
Thread thread = new Thread(stateTestThread);
System.out.println(thread.getName()+" state after constructor:"
+thread.getState());
try {
Thread.sleep(1000L);
thread.start();
Thread.sleep(1000L);
System.out.println(thread.getName()+" state after run:"
+thread.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

运行结果

「RUNNABLE」 -> 「TIMED_WAITING」

public class ThreadStateTest {    public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
public void run() {
long begin = System.currentTimeMillis();
for (int i = 0; i < (1 << 25); i++) {
int j = (int) Math.sqrt(i);
// 该条件永远不成立, 只是为了计算
if (j * j > i) {
break;
}
}
long end = System.currentTimeMillis();
System.out.println("calculate end:"+(end - begin));
try {
System.out.println("begin sleep");
Thread.sleep(5000L);
System.out.println("end sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
try {
thread.start();
Thread.sleep(100L);
System.out.println(thread.getName()+" state :"
+thread.getState());
Thread.sleep(1000L);
System.out.println(thread.getName()+" state :"
+thread.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

结果如下:

Thread-0 的状态从 「RUNNABLE」 转化为 「TIMED_WAITING」

「RUNNABLE」 -> 「WAITING」

public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
final Object lock = new Object();
public void run() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
}
});thread.start();
System.out.println(thread.getName()+" state :"
+thread.getState());
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(thread.getName()+" state :"
+thread.getState());
}

运行结果

「RUNNABLE」 -> 「BLOCKED」

先创建一个 Runnable 子类

public static void main(String[] args) {
Thread thread = new Thread(new Runnable() {
final Object lock = new Object();
public void run() {
synchronized (lock) {
try {
lock.wait();
} catch (InterruptedException e) {}
}
}
});thread.start();
System.out.println(thread.getName()+" state :"+thread.getState());
try {
Thread.sleep(1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(thread.getName()+" state :"+thread.getState());
}

测试方法

public static void main(String[] args) {
BlockedStateRun blockedStateRun = new BlockedStateRun();
Thread thread1= new Thread(blockedStateRun);
Thread thread2= new Thread(blockedStateRun);
thread1.setName("First");
thread1.start();
thread2.setName("Second");
thread2.start();
try {
Thread.sleep(200L);
System.out.println(thread1.getName()+"::"+thread1.getState());
System.out.println(thread2.getName()+"::"+thread2.getState());
} catch (InterruptedException e) {
e.printStackTrace();
}}

最后的运行结果:

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291