当前位置:网站首页>【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 略
边栏推荐
- 基于LSTM模型实现新闻分类
- 面试题:MySQL的union all和union有什么区别、MySQL有哪几种join方式(阿里面试题)[通俗易懂]
- MySQL empties table data
- 【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!
- Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
- [NOIP2013]积木大赛 [NOIP2018]道路铺设 贪心/差分
- 地图其他篇总目录
- Pytest Collection (2) - mode de fonctionnement pytest
- [intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
- [noip2013] building block competition [noip2018] road laying greed / difference
猜你喜欢

"The silk road is in its youth and looks at Fujian" is in the hot collection of works in the Fujian foreign youth short video competition

Go - exe corresponding to related dependency
Design and practice of new generation cloud native database

News classification based on LSTM model

杰理之烧录上层版物料需要【篇】

AirServer2022最新版功能介绍及下载

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

Do you want to make up for the suspended examination in the first half of the year? Including ten examinations for supervision engineers, architects, etc
![[deep learning] use deep learning to monitor your girlfriend's wechat chat?](/img/03/ecf50eacc91c0633b0d9689cdad2c2.png)
[deep learning] use deep learning to monitor your girlfriend's wechat chat?

按照功能对Boost库进行分类
随机推荐
AIDL基本使用
【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
Several ways of writing main function in C
基于K-means的用户画像聚类模型
Four methods of JS array splicing [easy to understand]
Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
编程英语生词笔记本
辅音和声母的区别?(声母与辅音的区别)
The difference between NiO and traditional IO
杰理之、产线装配环节【篇】
【生态伙伴】鲲鹏系统工程师培训
mysql 学习笔记-优化之SQL优化
基于LSTM模型实现新闻分类
Simple interactive operation of electron learning (III)
Why does blocprovider feel similar to provider?
BlocProvider 为什么感觉和 Provider 很相似?
【深度学习】利用深度学习监控女朋友的微信聊天?
基于YOLOv5的口罩佩戴检测方法
月入1W+的自媒体达人都会用到的运营工具
LIS (longest ascending subsequence) problem that can be understood [easy to understand]