当前位置:网站首页>Concurrent Park and unpark description
Concurrent Park and unpark description
2022-07-27 09:52:00 【Under the seven kingdoms, I want 99】
Concurrent park And unpark explain
1 park And unpark Use
park and unpark yes LockSupport Methods in class
// Pause current thread
LockSupport.park();
// Resume the running of a thread
LockSupport.unpark( Pause thread object )
Case study 1
First park after unpark
Thread t1 = new Thread(() -> {
log.debug("start...");
sleep(1);
log.debug("park...");
LockSupport.park();
log.debug("resume...");
},"t1");
t1.start();
sleep(2);
log.debug("unpark...");
LockSupport.unpark(t1);
/* Running results : 18:42:52.585 c.TestParkUnpark [t1] - start... 18:42:53.589 c.TestParkUnpark [t1] - park... 18:42:54.583 c.TestParkUnpark [main] - unpark... 18:42:54.583 c.TestParkUnpark [t1] - resume... */
According to the results :
t1 Thread sleep 1s After execution park Method , main Threads sleep 2s After execution unpark Method
Case study 2
First unpark after park
Thread t1 = new Thread(() -> {
log.debug("start...");
sleep(2);
log.debug("park...");
LockSupport.park();
log.debug("resume...");
}, "t1");
t1.start();
sleep(1);
log.debug("unpark...");
LockSupport.unpark(t1);
/* Running results : 18:43:50.765 c.TestParkUnpark [t1] - start... 18:43:51.764 c.TestParkUnpark [main] - unpark... 18:43:52.769 c.TestParkUnpark [t1] - park... 18:43:52.769 c.TestParkUnpark [t1] - resume... */
According to the results :
t1 Thread sleep 2s After execution park Method , And no sleep , Direct output statement , main Threads sleep 1s After execution unpark Method .
characteristic
And Object Medium wait and notify Methods to compare :
wait,notify and notifyAll Must cooperate Object Monitor Use it together , and park,unpark No need
park & unpark It's based on threads 【 Blocking 】 and 【 Wake up the 】 Threads , and notify Only one waiting thread can be wakened randomly ,notifyAll Is to wake up all waiting threads , It's not that 【 accurate 】
park & unpark You can start with unpark, and wait & notify Not first notify
2 park And unpark Principle
Each thread has its own Parker object , It's made up of three parts _counter , _cond and _mutex.
Take travelers for example :
- Thread is like a traveler ,Parker Like his backpack , Conditional variables are like tents in backpacks ._counter It's like in a backpack Spare dry food (0 To exhaust ,1 For the sake of sufficiency )
- call park It depends on whether you need to stop and rest
- If the spare dry food runs out , So get into the tent and rest
- If there's enough dry food in reserve , Then there is no need to stay , To move forward
- call unpark, It's like having enough dry food
- If the thread is still in the tent , Just wake him up and let him move on
- If the thread is still running , So next time he calls park when , Just consume spare dry food , No need to stop and move on
- Because the backpack space is limited , Multiple calls unpark Only one spare dry food will be added \
situation 1

1 Current thread call Unsafe.park() Method
2 Check _counter , This situation is 0, At this time , get _mutex The mutex
3 Thread entry _cond Conditional variables block
4 Set up _counter = 0
situation 2

1 call Unsafe.unpark(Thread_0) Method , Set up _counter by 1
2 Wake up the _cond In a conditional variable Thread_0
3 Thread_0 Resume operation
4 Set up _counter by 0
situation 3

1 call Unsafe.unpark(Thread_0) Method , Set up _counter by 1
2 Current thread call Unsafe.park() Method
3 Check _counter , This situation is 1, At this time, the thread does not need to block , Continue operation
4 Set up _counter by 0
边栏推荐
猜你喜欢

XML overview

How to restore the original version after installing Hal Library

二叉树习题总结

Expose a technology boss from a poor family

Lua function nested call

LeetCode.814. 二叉树剪枝____DFS
![[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination](/img/15/298ea6f7367741e1e085007c498e51.jpg)
[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination

Understand chisel language. 23. Chisel sequential circuit (III) -- detailed explanation of chisel shift register

安装了HAL库如何恢复原来的版本

华为交换机双上行组网Smart-link配置指南
随机推荐
Nacos configuration center dynamically refreshes the data source
面试必备:虾皮服务端15连问
省应急管理厅:广州可争取推广幼儿应急安全宣教经验
如何在树莓派上安装cpolar内网穿透
Redis 为什么这么快?Redis 的线程模型与 Redis 多线程
S交换机堆叠方案配置指南
July training (day 24) - segment tree
Food safety | the kitchen board environment is very important. Do you know these use details?
NFT系统开发-教程
华为交换机双上行组网Smart-link配置指南
7/26 thinking +dp+ suffix array learning
Vscode uses remote SSH connection and the solution of connection failure
QT | about the problem that QT creator cannot open the project and compile it
Qt 学习(二) —— Qt Creator简单介绍
[cloud native] how can I compete with this database?
系统架构之系统参数常量表:
Why do microservices have to have API gateways?
会议OA项目之会议排座功能&&会议送审的实现
在Centos 7安装Mysql 5.7.27后无法启动?(语言-bash)
The command prompt cannot start mysql, prompting system error 5. Access denied. terms of settlement