当前位置:网站首页>并发之park与unpark说明
并发之park与unpark说明
2022-07-27 09:43:00 【七国的天下,我要九十九】
并发之park与unpark说明
1 park与unpark使用
park和unpark是 LockSupport 类中的方法
// 暂停当前线程
LockSupport.park();
// 恢复某个线程的运行
LockSupport.unpark(暂停线程对象)
案例1
先park后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);
/* 运行结果: 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... */
根据结果可知:
t1线程睡眠1s后执行park方法, main线程 睡眠2s后执行unpark方法
案例2
先unpark后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);
/* 运行结果: 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... */
根据结果可知:
t1线程睡眠2s后执行park方法, 且没有睡眠,直接输出语句, main线程 睡眠1s后执行unpark方法.
特点
与Object中的wait和notify方法比较:
wait,notify 和 notifyAll 必须配合 Object Monitor 一起使用,而 park,unpark 不必
park & unpark 是以线程为单位来【阻塞】和【唤醒】线程,而 notify 只能随机唤醒一个等待线程,notifyAll 是唤醒所有等待线程,就不那么【精确】
park & unpark 可以先 unpark,而 wait & notify 不能先 notify
2 park与unpark的原理
每个线程都有自己的一个 Parker 对象,由三部分组成 _counter , _cond 和 _mutex.
以旅行者为例:
- 线程就像一个旅人,Parker 就像他随身携带的背包,条件变量就好比背包中的帐篷。_counter 就好比背包中 的备用干粮(0 为耗尽,1 为充足)
- 调用 park 就是要看需不需要停下来歇息
- 如果备用干粮耗尽,那么钻进帐篷歇息
- 如果备用干粮充足,那么不需停留,继续前进
- 调用 unpark,就好比令干粮充足
- 如果这时线程还在帐篷,就唤醒让他继续前进
- 如果这时线程还在运行,那么下次他调用 park 时,仅是消耗掉备用干粮,不需停留继续前进
- 因为背包空间有限,多次调用 unpark 仅会补充一份备用干粮\
情况1

1 当前线程调用 Unsafe.park() 方法
2 检查 _counter ,本情况为 0,这时,获得 _mutex 互斥锁
3 线程进入 _cond 条件变量阻塞
4 设置 _counter = 0
情况2

1 调用 Unsafe.unpark(Thread_0) 方法,设置 _counter 为 1
2 唤醒 _cond 条件变量中的 Thread_0
3 Thread_0 恢复运行
4 设置 _counter 为 0
情况3

1 调用 Unsafe.unpark(Thread_0) 方法,设置 _counter 为 1
2 当前线程调用 Unsafe.park() 方法
3 检查 _counter ,本情况为 1,这时线程无需阻塞,继续运行
4 设置 _counter 为 0
边栏推荐
- Interview Essentials: shrimp skin server 15 consecutive questions
- NCCL (NVIDIA Collective Communications Library)
- Brush the title "sword finger offer" day04
- XML overview
- 加油程序君
- 七月集训(第11天) —— 矩阵
- How to install cpolar intranet penetration on raspberry pie
- 七月集訓(第07天) —— 哈希表
- It's great to write code for 32 inch curved screen display! Send another one!
- Understand chisel language. 25. Advanced input signal processing of chisel (I) -- asynchronous input and de jitter
猜你喜欢

交换机端口镜像配置指南

How to install cpolar intranet penetration on raspberry pie

聊聊索引失效的10种场景,太坑了

深入浅出详解Knative云函数框架!

Interview JD T5, was pressed on the ground friction, who knows what I experienced?

Nacos is used as a registration center

Vscode uses remote SSH connection and the solution of connection failure

吃透Chisel语言.26.Chisel进阶之输入信号处理(二)——多数表决器滤波、函数抽象和异步复位

Understand chisel language. 22. Chisel sequential circuit (II) -- detailed explanation of chisel counter: counter, timer and pulse width modulation

XML概述
随机推荐
2016展望
[cloud native] how can I compete with this database?
NFT系统开发-教程
Final examination paper of engineering materials
吃透Chisel语言.23.Chisel时序电路(三)——Chisel移位寄存器(Shift Register)详解
July training (day 07) - hash table
Understand chisel language. 27. Chisel advanced finite state machine (I) -- basic finite state machine (Moore machine)
Nacos is used as a registration center
July training (day 19) - binary tree
七月集训(第04天) —— 贪心
Google Earth Engine APP——利用S2影像进行最大值影像合成分析
Esp8266 Arduino programming example ADC
Lua function nested call
华为交换机双上行组网Smart-link配置指南
Interview Essentials: shrimp skin server 15 consecutive questions
35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍
S switch stacking scheme configuration guide
七月集训(第06天) —— 滑动窗口
C# 给Word每一页设置不同文字水印
[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination