当前位置:网站首页>[interview: concurrent Article 19: multithreading: Park & unpark]
[interview: concurrent Article 19: multithreading: Park & unpark]
2022-07-21 17:07:00 【I cream】
【 interview : Concurrent articles 19: Multithreading :Park&Unpark】
00. Preface
If you have any questions, please point out , thank .
01. Introduce
Basic use
// Pause current thread
LockSupport.park();
// Resume the running of a thread
LockSupport.unpark( Pause thread object )
When used in a thread LockSupport.park(); This thread becomes WATING state
02. Example
@Slf4j(topic = "c.TestParkUnpark")
public class TestParkUnpark {
public static void main(String[] args) {
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);
}
}
result
17:22:16.403 c.TestParkUnpark [t1] - start…
17:22:17.412 c.TestParkUnpark [t1] - park…
17:22:18.402 c.TestParkUnpark [main] - unpark…
17:22:18.402 c.TestParkUnpark [t1] - resume…
explain
You can see that we are 1s when LockSupport.park(); Thread paused t1 stay 2s when LockSupport.unpark(t1); hold t1 Thread recovery
Be careful
If we were in 1s When it's time to recover t1 Threads ,2s Then pause t1 What happens to threads ?
@Slf4j(topic = "c.TestParkUnpark")
public class TestParkUnpark {
public static void main(String[] args) {
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);
}
}
result
17:27:11.949 c.TestParkUnpark [t1] - start…
17:27:12.959 c.TestParkUnpark [main] - unpark…
17:27:13.958 c.TestParkUnpark [t1] - park…
17:27:13.958 c.TestParkUnpark [t1] - resume…
explain
It can be seen that we first unpark Again park,t1 The thread is still running normally , With us wait/notify The understanding is different
03. characteristic
LockSupport Of park/unpark And Object Of wait/notify comparison :
1.wait notify and notifyAll Must cooperate Object Monitor Use together, that is, it can only be used under the condition of heavyweight lock , and park unpark Unwanted
2.park unpark Is to block and wake up threads on a thread by thread basis The tag can accurately wake up the specified waiting thread , and notify Only one waiting thread can be wakened randomly notifyAll Is to wake up all waiting threads , Imprecise
3.park unpark You can start with unpark, however wait notify Not first notify
04. principle
Each thread has its own Parker object , It's made up of three parts _counter, _ cond and _mtuex, Make a comparison
Thread is like a traveller ,Parker It's the backpack it carries , Conditional variables are like tents in backpacks ,_ counter It's like spare dry food in a backpack (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 , Then go to the tent to rest
If there's enough dry food in reserve , Then there's no need to stay To move forward
call unpark, It's like having enough dry food
If the thread is still in the tent Wake him up for so long 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
Premise : Not yet park, To explore the park Changes in the process
1. Current thread call park Method
2. Check _counter This situation is 0 here get _mutex The mutex
3. Thread entry _cond Conditional variables block
4. Set up _counter=0
Premise : At this point has been park After that , To explore the unpark Changes in the process
1. call 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
Premise : Explore first unpark after park The situation of
1. call unpark(Thread_0) Method , Set up _counter by 1
2. Current thread call park Method
3. Check _counter This situation is 1 At this point, the thread does not need to block Continue operation
4. Set up _counter by 0
边栏推荐
- 1744. Can you eat your favorite candy on your favorite day?
- First day of scala study (Hello World)
- Scala学习第一天(Hello world)
- I heard you want to play minesweeping on your mobile phone? Native JS minesweeping game
- 【保研】-- 保研夏令营中开放性问题回答
- LNMP------PHP7安装
- kubernetes部署mysql5.7(单节点)
- kube-Controller Manager 原理
- 1744. 你能在你最喜欢的那天吃到你最喜欢的糖果吗?
- 花6000报了测试培训班,3个月后我成功“骗”进了腾讯,月薪拿17k
猜你喜欢
Classic automated interview questions
王者荣耀商城异地多活架构设计(架构实战营 模块七作业)
Okaleido tiger NFT is about to log in to the binance NFT platform. Are you looking forward to it?
938. 二叉搜索树的范围和
Introduction software testing tips
How to learn automated testing from scratch?
GAMES101图形学P10笔记(geometry1)
1011. Ability to deliver packages within D days
First meet shadowless cloud computer
消息队列-rockerMQ开发实战
随机推荐
1723. Minimum time to complete all work
听说你想手机玩扫雷?原生JS扫雷游戏
Large language models teach agents to evolve, and openai reveals the complementary relationship between the two
GAMES101图形学P11笔记(geometry2)
1011. 在 D 天内送达包裹的能力
403. 青蛙过河
Apple Mobile App full screen
项目经理如何做好“老板”项目
Swift 中监听属性值变化 (观察者模式)
APP 页面秒开优化方面总结~
相关性分析及SPSS软件操作
渗透测试成功的8个关键
redis集群安装
1744. Can you eat your favorite candy on your favorite day?
How to do well in test management?
554. Brick wall
Multi active architecture design of wangzhe glory Mall (module 7 of architecture practice camp)
8 keys to successful penetration testing
ABAP语法基础
Redis内存模型讲解