当前位置:网站首页>并发之线程状态转换
并发之线程状态转换
2022-07-27 09:43:00 【七国的天下,我要九十九】
并发之线程状态转换
根据之前整理的线程状态转换图来说明其转换信息.

情况1 NEW --> RUNNABLE
当调用t.start()方法时, 由 NEW --> RUNNABLE
情况2 RUNNABLE <–> WAITING
t 线程用 synchronized(obj) 获取了对象锁后
- 调用 obj.wait() 方法时,t 线程从 RUNNABLE --> WAITING
- 调用 obj.notify() , obj.notifyAll() , t.interrupt() 时
- 竞争锁成功,t 线程从 WAITING --> RUNNABLE
- 竞争锁失败,t 线程从 WAITING --> BLOCKED
public class TestWaitNotify {
final static Object obj = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (obj) {
log.debug("执行....");
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("其它代码...."); // 断点
}
},"t1").start();
new Thread(() -> {
synchronized (obj) {
log.debug("执行....");
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.debug("其它代码...."); // 断点
}
},"t2").start();
sleep(0.5);
log.debug("唤醒 obj 上其它线程");
synchronized (obj) {
obj.notifyAll(); // 唤醒obj上所有等待线程 断点
}
}
}
/* 运行结果: 08:10:09.924 c.TestWaitNotify [t1] - 执行.... 08:10:09.939 c.TestWaitNotify [t2] - 执行.... 08:10:10.346 c.TestWaitNotify [main] - 唤醒 obj 上其它线程 08:10:10.346 c.TestWaitNotify [t2] - 其它代码.... 08:10:10.346 c.TestWaitNotify [t1] - 其它代码.... */
说明:
1 当线程t1和t2先后竞争到锁, 执行了wait方法,状态由RUNNABLE --> WAITING , 等待并且释放锁.
2 主线程睡眠0.5秒后,竞争到锁,执行notifyAll方法,唤醒所有其他线程
3 t1和t2线程竞争锁, 一个竞争成功,状态由WAITING --> RUNNABLE, 另一个竞争失败,状态由WAITING --> BLOCKED
4 t线程执行完后释放锁, 另外一个线程竞争到锁,开始执行,执行完成后,线程由RUNNABLE <–> TERMINATED
情况 3 RUNNABLE <–> WAITING
当前线程调用 t.join() 方法时,当前线程从 RUNNABLE --> WAITING
- 是当前线程在t 线程对象的监视器上等待
t 线程运行结束,或调用了当前线程的 interrupt() 时,当前线程从 WAITING --> RUNNABLE
情况 4 RUNNABLE <–> WAITING
当前线程调用 LockSupport.park() 方法会让当前线程从 RUNNABLE --> WAITING
调用 LockSupport.unpark(目标线程) 或调用了线程 的 interrupt() ,会让目标线程从 WAITING --> RUNNABLE
情况 5 RUNNABLE <–> TIMED_WAITING
t 线程用 synchronized(obj) 获取了对象锁后
- 调用 obj.wait(long n) 方法时,t 线程从 RUNNABLE --> TIMED_WAITING
- t 线程等待时间超过了 n 毫秒,或调用 obj.notify() , obj.notifyAll() , t.interrupt() 时
- 竞争锁成功,t 线程从 TIMED_WAITING --> RUNNABLE
- 竞争锁失败,t 线程从 TIMED_WAITING --> BLOCKED
情况 6 RUNNABLE <–> TIMED_WAITING
当前线程调用 t.join(long n) 方法时,当前线程从 RUNNABLE --> TIMED_WAITING
- 是当前线程在t 线程对象的监视器上等待
当前线程等待时间超过了 n 毫秒,或t 线程运行结束,或调用了当前线程的 interrupt() 时,当前线程从 TIMED_WAITING --> RUNNABLE
情况 7 RUNNABLE <–> TIMED_WAITING
当前线程调用 Thread.sleep(long n) ,当前线程从 RUNNABLE --> TIMED_WAITING
当前线程等待时间超过了 n 毫秒,当前线程从 TIMED_WAITING --> RUNNABLE
情况 8 RUNNABLE <–> TIMED_WAITING
当前线程调用 LockSupport.parkNanos(long nanos) 或 LockSupport.parkUntil(long millis) 时,当前线 程从 RUNNABLE --> TIMED_WAITING
调用 LockSupport.unpark(目标线程) 或调用了线程 的 interrupt() ,或是等待超时,会让目标线程从 TIMED_WAITING–> RUNNABLE
情况 9 RUNNABLE <–> BLOCKED
t 线程用 synchronized(obj) 获取了对象锁时如果竞争失败,从 RUNNABLE --> BLOCKED
持 obj 锁线程的同步代码块执行完毕,会唤醒该对象上所有 BLOCKED 的线程重新竞争,如果其中 t 线程竞争 成功,从 BLOCKED --> RUNNABLE ,其它失败的线程仍然 BLOCKED
情况 10 RUNNABLE <–> TERMINATED
当前线程所有代码运行完毕,进入 TERMINATED
边栏推荐
- 加油程序君
- July training (day 14) - stack
- Quickly apply JS to customize the effect of lunar phase change
- Engineering survey simulation volume a
- July training (day 15) - depth first search
- 2016展望
- Talk about 10 scenarios of index failure. It's too stupid
- About getter/setter methods
- Brush the title "sword finger offer" day03
- Understand chisel language. 26. Chisel advanced input signal processing (II) -- majority voter filtering, function abstraction and asynchronous reset
猜你喜欢

好久不送书,浑身不舒服

Eureka delayed registration of a pit

NFT系统开发-教程

会议OA项目之会议排座功能&&会议送审的实现

Redis 为什么这么快?Redis 的线程模型与 Redis 多线程

Quick apply custom progress bar

Proposed relocation! 211 the new campus of China University of Petroleum (East China) is officially opened!
![[cloud native • Devops] master the container management tool rancher](/img/6f/50deaf053c86486e52d2c2c7310ed2.png)
[cloud native • Devops] master the container management tool rancher

flash闪存使用和STM32CUBEMX安装教程【第三天】

Voice live broadcast system - Principles to be followed in developing push notifications
随机推荐
如何在树莓派上安装cpolar内网穿透
好久不送书,浑身不舒服
Exercises --- quick arrangement, merging, floating point number dichotomy
注解与反射
July training (day 17) - breadth first search
Engineering survey simulation volume a
Understand chisel language. 27. Chisel advanced finite state machine (I) -- basic finite state machine (Moore machine)
How to install cpolar intranet penetration on raspberry pie
July training (day 12) - linked list
Expose a technology boss from a poor family
Brush the title "sword finger offer" day04
Understand chisel language. 24. Chisel sequential circuit (IV) -- detailed explanation of chisel memory
Meeting seating function of conference OA project & Implementation of meeting submission for approval
July training (day 18) - tree
Monitoring artifact: Prometheus easy to get started, really fragrant!
快应用自定义进度条
Lua function nested call
35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction
C # set different text watermarks for each page of word
wordpress禁止指定用户名登录或注册插件【v1.0】