当前位置:网站首页>[JUC learning road day 8] condition
[JUC learning road day 8] condition
2022-07-01 22:55:00 【birdyson】
StampedLock
Implement the locking mechanism according to the timestamp , Used to solve the read-write lock when the number of read-write resource threads is unbalanced , The unfair situation of resource preemption . There are three lock processing modes supported in this class , Write lock respectively 、 Pessimistic locking 、 Optimism lock . Every complete StampedLock It is composed of version and mode , When acquiring the relevant lock, a digital tag stamp will be returned , Used to control the state of the lock , And the mark stamp is used to realize the unlocking processing .
Condition
When I first learned multithreading technology , Will use Object Class wait、notify Method to wait and wake up , Then in the traditional Thread Among classes , It also provides pause and resume functions ( This function has been abolished ),Condition It achieves the same function .
Condition It's an interface , Can rely on Lock Interface to implement .public Condition newCondition()
stay ReentranLock It can be found in the class that this method is composed of
sync.newCondition()Method called ;sync The object is AQS Implementation subclass , So the final
newcondition()The way is by AQS To complete ;public Condition newCondition() { return sync.newCondition(); }sync Implemented in the
new ConditionObject(), This is a AQS The inner class of , The implementation is as follows :public class ConditionObject implements Condition, java.io.Serializable { private static final long serialVersionUID = 1173984872572414699L; private transient ConditionNode firstWaiter; private transient ConditionNode lastWaiter; public ConditionObject() { }After analysis, we can find , stay Condition Internally, all waiting operation threads are actually saved in the queue , Because we can rely on AQS Provided CLH Mechanism to realize thread control .
Condition Realize thread waiting and wakeup :
package juc.condition2;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/** * @author birdy * @date 2022/6/25 10:58 AM */
public class Main {
public static String msg = null;
public static ReentrantLock reentrantLock = new ReentrantLock();
public static Condition condition = reentrantLock.newCondition();
public static void main(String[] args) {
try {
reentrantLock.lock();
new Thread(() -> {
try {
reentrantLock.lock();
TimeUnit.MILLISECONDS.sleep(2);
System.out.println("sub thread complete!");
msg = "hello!";
condition.signal();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
reentrantLock.unlock();
}
}).start();
condition.await();
} catch (Exception e) {
e.printStackTrace();
} finally {
reentrantLock.unlock();
System.out.println("unlocked");
}
System.out.println(msg);
}
}
LockSupport A little
边栏推荐
- Cloud Vulnerability Global Database
- Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
- Explain kubernetes network model in detail
- Rust language - Introduction to Xiaobai 05
- Pytorch nn.functional.unfold()的简单理解与用法
- ESP自动下载电路设计
- Yolov5.5 call local camera
- 深度学习--数据操作
- 【扫盲】机器学习图像处理中的深层/浅层、局部/全局特征
- Metauniverse may become a new direction of Internet development
猜你喜欢

Reprint CSDN article operation

104. SAP ui5 table control supports multi select and how to select multiple table row items at a time with code

Genicam gentl standard ver1.5 (4) Chapter 5 acquisition engine

今日睡眠质量记录71分

Explain ThreadLocal in detail

MySQL5.7 设置密码策略(等保三级密码改造)

Stimulate new kinetic energy and promote digital economy in multiple places

Appium自动化测试基础 — APPium安装(一)

Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load

Single step debugging analysis of rxjs observable of operator
随机推荐
Origin2018 installation tutorial "recommended collection"
Understanding of inverted residuals
Rust语言——小小白的入门学习05
ECMAScript 2022 正式发布,有你了解过的吗?
使用 Three.js 实现'雪糕'地球,让地球也凉爽一夏
多图预警~ 华为 ECS 与 阿里云 ECS 对比实战
旅游管理系统
Demo program implementation of QT version Huarui camera
Fiori 应用通过 Adaptation Project 的增强方式分享
cvpr2022 human pose estiamtion
Quantifiers of regular series
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…
Cut noodles C language
第三方验收测试有什么好处?专业第三方软件测试机构推荐
nn.Parameter】Pytorch特征融合自适应权重设置(可学习权重使用)
Cloud Vulnerability Global Database
转--原来gdb的底层调试原理这么简单
[literacy] deep / shallow, local / global features in machine learning image processing
[MySQL] index classification
死锁的处理策略—预防死锁、避免死锁、检测和解除死锁