Java充电社
专辑
博文
联系我
本人继续续收门徒,亲手指导
Java高并发教程
-> 线程6种状态详解
1、必须知道的几个概念
2、并发级别
3、有关并行的两个重要定律
4、JMM相关的一些概念
5、深入理解进程和线程
6、线程的基本操作
7、volatile与Java内存模型
8、线程组
9、用户线程和守护线程
10、线程安全和synchronized
11、中断线程的几种方式
12、JUC中ReentrantLock
13、JUC中的Condition
14、JUC中的LockSupport工具类
15、UC中的Semaphore(信号量)
16、JUC中的CountDownLatch
17、JUC中的循环栅栏CyclicBarrier
18、线程池
19、JUC中的Executor框架详解1
20、JUC中的Executor框架详解2
21、java中的CAS
22、java中的UnSafe类
23、JUC中的原子操作类
24、ThreadLocal、InheritableThreadLocal
25、JUC中的阻塞队列
26、JUC中一些常见的集合
27、实战:你的接口太慢了需要优化
28、实战:构建日志系统
29、实战:一起来搞懂限流
30、JUC中的CompletableFuture
31、等待线程完成的方式你知道几种?
32、原子操作增强类LongAdder、LongAccumulator
33、怎么演示公平锁和非公平锁
34、谷歌提供的一些好用的并发工具类
35、延迟队列 DelayQueue 详解
36、线程6种状态详解
37、如何实现一个通用的延迟队列?
上一篇:延迟队列 DelayQueue 详解
下一篇:如何实现一个通用的延迟队列?
<div style="display:none"></div> **线程的状态有哪几种?他们之间是如何转换的?** ## 目录 [TOC] ## 1、线程状态分类 线程一共有六种状态,分别为New、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING、TERMINATED,同一时刻只有一种状态,通过线程的getState方法可以获取线程的状态。 ## 2、状态详解 Thread的状态使用java.lang.Thread.State枚举表示。 ### 2.1、状态1:NEW 当线程被创建出来还没有被调用start()时候的状态。 ```java public class NewState { public static void main(String[] args) { Thread thread1 = new Thread("thread1"); System.out.println(thread1.getState()); } } ``` 输出 ``` NEW ``` ### 2.2、状态2:RUNNABLE 当线程被调用了start(),且处于等待操作系统分配资源(如CPU)、等待IO连接、正在运行状态,即表示Running状态和Ready状态。 注:不一定被调用了start()立刻会改变状态,还有一些准备工作,这个时候的状态是不确定的。 ```java public class RunnableState { public static void main(String[] args) { Thread thread1 = new Thread("thread1"); thread1.start(); System.out.println(thread1.getState()); } } ``` 输出 ``` RUNNABLE ``` ### 2.3、状态3:BLOCKED 等待监视器锁而被阻塞的线程的线程状态,当进入synchronized块/方法或者在调用wait()被唤醒/超时之后重新进入synchronized块/方法,但是锁被其它线程占有,这个时候被操作系统挂起,状态为阻塞状态。 阻塞状态的线程,即使调用interrupt()方法也不会改变其状态。 下面看案例代码,thread1持有lock对象的锁一直没有释放,而thread2也想获取lock对象的锁,但是锁一直被thread1持有者,导致thread2被阻塞在@1处,此时thread2的状态就是BLOCKED状态。 ```java import java.util.concurrent.TimeUnit; public class BlockedState { static String lock = "锁"; public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { synchronized (lock) { //死循环导致thread1一直持有lock对象锁 while (true) ; } } }; thread1.start(); //休眠1秒,让thread1先启动 TimeUnit.SECONDS.sleep(1); Thread thread2 = new Thread("thread2") { @Override public void run() { synchronized (lock) { //@1 System.out.println("thread2"); } } }; thread2.start(); System.out.println("thread1.state:" + thread1.getState()); System.out.println("thread2.state:" + thread2.getState()); } } ``` 运行结果 ```java thread1.state:RUNNABLE thread2.state:BLOCKED ``` 2个线程的堆栈信息,线程堆栈信息中包含了线程的详细信息,如:线程状态、线程目前执行到哪段代码了 ```java "thread2" #13 prio=5 os_prio=0 tid=0x00000000281ec000 nid=0x878 waiting for monitor entry [0x0000000028dff000] java.lang.Thread.State: BLOCKED (on object monitor) at BlockedState$2.run(BlockedState.java:25) - waiting to lock <0x00000007176b2a20> (a java.lang.String) Locked ownable synchronizers: - None "thread1" #12 prio=5 os_prio=0 tid=0x00000000281ea800 nid=0x5e50 runnable [0x0000000028cff000] java.lang.Thread.State: RUNNABLE at BlockedState$1.run(BlockedState.java:12) - locked <0x00000007176b2a20> (a java.lang.String) Locked ownable synchronizers: - None ``` ### 2.4、状态4:WAITING 无条件等待,当线程调用wait()/join()/LockSupport.park()不加超时时间的方法之后所处的状态,如果没有被唤醒或等待的线程没有结束,那么将一直等待,当前状态的线程不会被分配CPU资源和持有锁. **简单理解:就是无限期等待。** 导致线程处于WAITING有3中方式。 #### 方式1:wait() ```java import java.util.concurrent.TimeUnit; public class WaitingState1 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { synchronized (WaitingState1.class) { try { //调用wait方法,让线程等待 WaitingState1.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread1.start(); //模拟休眠1秒,让thread1运行到wait方法处 TimeUnit.SECONDS.sleep(1); System.out.println("thread.state:" + thread1.getState()); } } ``` 输出 ```java thread1.state:WAITING ``` 线程thread1堆栈信息 ```java "thread1" #12 prio=5 os_prio=0 tid=0x0000000027a5d800 nid=0x1b48 in Object.wait() [0x0000000028dbe000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000007176adc68> (a java.lang.Class for WaitingState1) at java.lang.Object.wait(Object.java:502) at WaitingState1$1.run(WaitingState1.java:11) - locked <0x00000007176adc68> (a java.lang.Class for WaitingState1) Locked ownable synchronizers: - None ``` #### 方式2:join() ```java public class WaitingState2 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { while (true) ; } }; thread1.start(); //join方法会让当前主线程等待thread1结束 thread1.join(); } } ``` 上面代码会导致主线程处于WAITING状态,下面是主线程堆栈信息,第2行显示主线程处于WAITING状态 ```java "main" #1 prio=5 os_prio=0 tid=0x00000000035a4000 nid=0x3fbc in Object.wait() [0x000000000305f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000007176b0708> (a WaitingState2$1) at java.lang.Thread.join(Thread.java:1249) - locked <0x00000007176b0708> (a WaitingState2$1) at java.lang.Thread.join(Thread.java:1323) at WaitingState2.main(WaitingState2.java:14) Locked ownable synchronizers: - None ``` #### 方式3:LockSupport.park() ```java import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; public class WaitingState3 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { LockSupport.park(); } }; thread1.start(); //模拟休眠1秒,让thread1运行到park方法处 TimeUnit.SECONDS.sleep(1); System.out.println("thread1.state:" + thread1.getState()); } } ``` 输出 ```java thread1.state:WAITING ``` 线程thread1堆栈信息 ```java "thread1" #12 prio=5 os_prio=0 tid=0x00000000287cc000 nid=0x3880 waiting on condition [0x000000002918f000] java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method) at java.util.concurrent.locks.LockSupport.park(LockSupport.java:304) at WaitingState3$1.run(WaitingState3.java:9) Locked ownable synchronizers: - None ``` ### 2.5、状态5:TIMED_WAITING 有条件的等待,当线程调用sleep(睡眠时间)/wait(等待时间)/join(等待时间)/ LockSupport.parkNanos(等待时间)/LockSupport.parkUntil(等待时间)方法之后所处的状态,在指定的时间没有被唤醒或者等待线程没有结束,会被系统自动唤醒,正常退出。 **简单点理解:有限期等待。** 导致线程处于WAITING有5中方式。 #### 方式1:sleep(睡眠时间) ```java import java.util.concurrent.TimeUnit; public class TimedWaitingState1 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { //休眠500秒 = 500000毫秒 try { Thread.sleep(500 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } }; thread1.start(); //模拟休眠1秒,让thread1运行到sleep方法处 TimeUnit.SECONDS.sleep(1); System.out.println("thread1.state:" + thread1.getState()); } } ``` 输出 ``` thread1.state:TIMED_WAITING ``` 线程thread1堆栈信息,堆栈信息中可以看出是线程sleep方法导致线程等待的 ```java "thread1" #12 prio=5 os_prio=0 tid=0x0000000027e1c000 nid=0x5c68 waiting on condition [0x000000002917f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at TimedWaitingState1$1.run(TimedWaitingState1.java:10) Locked ownable synchronizers: - None ``` #### 方式2:wait(等待时间) ```java import java.util.concurrent.TimeUnit; public class TimedWaitingState2 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { synchronized (TimedWaitingState2.class) { try { //调用wait方法,让线程等待500秒 TimedWaitingState2.class.wait(500 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }; thread1.start(); //模拟休眠1秒,让thread1运行到wait方法处 TimeUnit.SECONDS.sleep(1); System.out.println("thread1.state:" + thread1.getState()); } } ``` 输出 ```java thread1.state:TIMED_WAITING ``` 线程thread1堆栈信息,堆栈信息中可以看出是线程wait方法导致线程等待的 ```java "thread1" #12 prio=5 os_prio=0 tid=0x0000000028571000 nid=0x4b80 in Object.wait() [0x0000000028f2e000] java.lang.Thread.State: TIMED_WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000007176ae0b8> (a java.lang.Class for TimedWaitingState2) at TimedWaitingState2$1.run(TimedWaitingState2.java:11) - locked <0x00000007176ae0b8> (a java.lang.Class for TimedWaitingState2) Locked ownable synchronizers: - None ``` #### 方式3:join(等待时间) ```java public class TimedWaitingState3 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { while (true) ; } }; thread1.start(); //join方法会让当前主线程等待thread1结束,最长等待500s,如果500s thread1.join(500 * 1000); } } ``` 主线程堆栈信息 ```java "main" #1 prio=5 os_prio=0 tid=0x0000000003274000 nid=0x1204 in Object.wait() [0x00000000030ee000] java.lang.Thread.State: TIMED_WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x00000007176b0cf8> (a TimedWaitingState3$1) at java.lang.Thread.join(Thread.java:1257) - locked <0x00000007176b0cf8> (a TimedWaitingState3$1) at TimedWaitingState3.main(TimedWaitingState3.java:11) Locked ownable synchronizers: - None ``` #### 方式4:LockSupport.parkNanos(等待时间) ```java import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; public class TimedWaitingState4 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { //等待500秒 LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(500)); } }; thread1.start(); //模拟休眠1秒,让thread1运行到parkNanos方法处 TimeUnit.SECONDS.sleep(1); System.out.println("thread1.state:" + thread1.getState()); } } ``` 输出 ```java thread1.state:TIMED_WAITING ``` 线程thread1堆栈信息 ```java "thread1" #12 prio=5 os_prio=0 tid=0x0000000028a1e000 nid=0x455c waiting on condition [0x00000000293ff000] java.lang.Thread.State: TIMED_WAITING (parking) at sun.misc.Unsafe.park(Native Method) at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:338) at TimedWaitingState4$1.run(TimedWaitingState4.java:10) Locked ownable synchronizers: - None ``` #### 方式5:LockSupport.parkUntil(等待时间) ```java import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; public class TimedWaitingState5 { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { //等待500秒 LockSupport.parkUntil(System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(500)); } }; thread1.start(); //模拟休眠1秒,让thread1运行到parkNanos方法处 TimeUnit.SECONDS.sleep(1); System.out.println("thread1.state:" + thread1.getState()); } } ``` 输出 ``` thread1.state:TIMED_WAITING ``` 线程thread1堆栈信息 ```java "thread1" #12 prio=5 os_prio=0 tid=0x00000000291b2000 nid=0x3cc0 waiting on condition [0x0000000029b8f000] java.lang.Thread.State: TIMED_WAITING (parking) at sun.misc.Unsafe.park(Native Method) at java.util.concurrent.locks.LockSupport.parkUntil(LockSupport.java:372) at TimedWaitingState5$1.run(TimedWaitingState5.java:10) Locked ownable synchronizers: - None ``` ### 2.6、状态6:TERMINATED 执行完了run()方法。其实这只是Java语言级别的一种状态,在操作系统内部可能已经注销了相应的线程,或者将它复用给其他需要使用线程的请求,而在Java语言级别只是通过Java代码看到的线程状态而已。 ```java import java.util.concurrent.TimeUnit; public class TerminatedState { public static void main(String[] args) throws InterruptedException { Thread thread1 = new Thread("thread1") { @Override public void run() { System.out.println(Thread.currentThread()); } }; thread1.start(); //休眠1秒,等待thread1执行完毕 TimeUnit.SECONDS.sleep(1); System.out.println("thread1 state:" + thread1.getState()); } } ``` 输出 ```java Thread[thread1,5,main] thread1 state:TERMINATED ``` ## 3、状态转换图 这个图是本文的重点,反复看,消化理解!!! ![](https://itsoku.oss-cn-hangzhou.aliyuncs.com/itsoku/blog/article/187/8ca44f13-014e-4682-869c-f615b576ba94.jpg) ## 4、阿里最新版《Java 开发手册》 java 开发者必读的手册,阿里最新版《Java 开发规范》来了,本手册是阿里巴巴集团技术团队的集体智慧结晶和经验总结,愿景是码出高效, 码出质量。 **获取方式,扫码发送:阿里** ![](https://itsoku.oss-cn-hangzhou.aliyuncs.com/itsoku/blog/article/187/1c7cef88-744a-422f-96dd-936be3997c93.png) <a style="display:none" target="_blank" href="https://mp.weixin.qq.com/s/_S1DD2JADnXvpexxaBwLLg" style="color:red; font-size:20px; font-weight:bold">继续收门徒,亲手带,月薪 4W 以下的可以来找我</a> ## 最新资料 1. <a href="https://mp.weixin.qq.com/s?__biz=MzkzOTI3Nzc0Mg==&mid=2247484964&idx=2&sn=c81bce2f26015ee0f9632ddc6c67df03&scene=21#wechat_redirect" target="_blank">尚硅谷 Java 学科全套教程(总 207.77GB)</a> 2. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484192&idx=1&sn=505f2faaa4cc911f553850667749bcbb&scene=21#wechat_redirect" target="_blank">2021 最新版 Java 微服务学习线路图 + 视频</a> 3. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484573&idx=1&sn=7f3d83892186c16c57bc0b99f03f1ffd&scene=21#wechat_redirect" target="_blank">阿里技术大佬整理的《Spring 学习笔记.pdf》</a> 4. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247484544&idx=2&sn=c1dfe907cfaa5b9ae8e66fc247ccbe84&scene=21#wechat_redirect" target="_blank">阿里大佬的《MySQL 学习笔记高清.pdf》</a> 5. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485167&idx=1&sn=48d75c8e93e748235a3547f34921dfb7&scene=21#wechat_redirect" target="_blank">2021 版 java 高并发常见面试题汇总.pdf</a> 6. <a href="https://mp.weixin.qq.com/s?__biz=MzkwOTAyMTY2NA==&mid=2247485664&idx=1&sn=435f9f515a8f881642820d7790ad20ce&scene=21#wechat_redirect" target="_blank">Idea 快捷键大全.pdf</a> ![](https://itsoku.oss-cn-hangzhou.aliyuncs.com/itsoku/blog/article/1/2883e86e-3eff-404a-8943-0066e5e2b454.png)
#custom-toc-container