当前位置:网站首页>【juc学习之路第8天】Condition
【juc学习之路第8天】Condition
2022-07-01 21:43:00 【birdyson】
StampedLock
依据时间戳实现锁机制,用于解决读写锁在读写资源线程数量不平衡时,资源抢占不公平的情况。在该类中支持有三种锁的处理模式,分别为写锁、悲观锁、乐观锁。每一个完整的StampedLock是由版本和模式两个部分所组成,在获取相关锁时会返回有一个数字标记戳,用于控制锁的状态,并利用该标记戳实现解锁的处理。
Condition
在最早学习到多线程技术的时候,会使用Object类之中提供的 wait、notify方法进行等待与唤醒的处理,而后在传统的Thread 类之中,又提供了暂停和恢复的功能(这个功能已经被废除),Condition实现了相同的功能。
Condition是一个接口,可以依靠Lock接口提供的方法来实现。public Condition newCondition()
在 ReentranLock 类之中可以发现该方法是由
sync.newCondition()方法调用的;sync对象为 AQS 实现子类,所以最终的
newcondition()方法是由 AQS 来完成的;public Condition newCondition() { return sync.newCondition(); }sync中实现了
new ConditionObject(),这是AQS的内部类,实现如下:public class ConditionObject implements Condition, java.io.Serializable { private static final long serialVersionUID = 1173984872572414699L; private transient ConditionNode firstWaiter; private transient ConditionNode lastWaiter; public ConditionObject() { }经过分析之后可以发现,在Condition内部实际上是将所有的等待的操作线程保存在了队列之中,因为可以依靠AQS提供的CLH机制实现线程的控制。
Condition实现线程的等待与唤醒:
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 略
边栏推荐
- 【MySQL】索引的创建、查看和删除
- 使用闭包实现点击按钮切换 toggle
- 杰理之蓝牙耳机品控和生产技巧【篇】
- JS how to get a list of elements in a collection object
- Electron学习(三)之简单交互操作
- BlocProvider 为什么感觉和 Provider 很相似?
- [commercial terminal simulation solution] Shanghai daoning brings you Georgia introduction, trial and tutorial
- Qtreeview+qabstractitemmodel custom model: the third of a series of tutorials [easy to understand]
- Show member variables and methods in classes in idea
- Separate the letters and numbers in the string so that the letters come first and the array comes last
猜你喜欢

杰理之蓝牙耳机品控和生产技巧【篇】

pytest合集(2)— pytest运行方式

Sonic云真机学习总结6 - 1.4.1服务端、agent端部署

首席信息官对高绩效IT团队定义的探讨和分析

上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
![[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
Design and practice of new generation cloud native database

杰理之、产线装配环节【篇】

小 P 周刊 Vol.11

List announced | outstanding intellectual property service team in China in 2021
随机推荐
为什么数字化转型战略必须包括持续测试?
统计字符中每个字符出现的个数
Icml2022 | interventional contrastive learning based on meta semantic regularization
AirServer2022最新版功能介绍及下载
基础—io密集型计算和cpu密集型计算
从MLPerf谈起:如何引领AI加速器的下一波浪潮
【MySQL】索引的创建、查看和删除
vscode的使用
Classify boost libraries by function
Training on the device with MIT | 256Kb memory
Flume interview questions
The difference between NiO and traditional IO
Unity uses SQLite
pytest合集(2)— pytest運行方式
【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
Mask wearing detection method based on yolov5
Spark面试题
Difference and use between require and import
手动实现function isInstanceOf(child,Parent)
Design and practice of new generation cloud native database