@

线程状态 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();
} }

最后的运行结果:

最新文章

  1. jasmine入门
  2. DataBinding examples
  3. 修改Linux可显示的行数
  4. Android新旧版本Notification
  5. react.js 之 批量添加与删除功能
  6. 淘宝主搜索离线集群完成Hadoop 2
  7. 前台JSP页面独立化
  8. X86(32位)与X64(64位)有什么区别,如何选择对应的操作系统和应用程序?
  9. C#界面设计疑问
  10. Erlang常用代码段
  11. js 实现图片压缩并转换成base64(data:image/jpeg;base64)格式
  12. hihoCoder #1053 : 居民迁移(贪心,二分搜索,google在线技术笔试模拟)
  13. Java中 equals() 和 == 的区别
  14. C#之PixturBox控件实现缩放和拖动图片
  15. 网络流之最小费用最大流 P1251 餐巾计划问题
  16. html与ios交互方法 WebViewJavascriptBridge
  17. react canvas圆环动态百分比
  18. RBS SharePoint 2010 Server.wmv
  19. 史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)
  20. django+celery +rabbitmq

热门文章

  1. Innodb页面存储结构-2
  2. nc 命令使用详解
  3. python urllib库
  4. How to Be Assertive Asking for What You Want Firmly and Fairly
  5. 常用js对象、数组、字符串的方法
  6. 端口扫描--zmap
  7. oracle kill 锁
  8. Javascript中的Form表单知识点总结
  9. nodeSelector + deamonset
  10. jqgrid 启用键盘操作bindKeys