当前位置:网站首页>【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系列之事务日志Redo log学习笔记
- pytest合集(2)— pytest運行方式
- K-means based user portrait clustering model
- Show member variables and methods in classes in idea
- Four methods of JS array splicing [easy to understand]
- JS how to get a list of elements in a collection object
- Manually implement function isinstanceof (child, parent)
- C中main函数的几种写法
- I received a letter from CTO inviting me to interview machine learning engineer
- 测试撤销1
猜你喜欢
MIT|256KB 内存下的设备上训练
[intelligent QBD risk assessment tool] Shanghai daoning brings you leanqbd introduction, trial and tutorial
基于YOLOv5的口罩佩戴检测方法
微软、哥伦比亚大学|GODEL:目标导向对话的大规模预训练
Pytest Collection (2) - mode de fonctionnement pytest
MySQL learning notes - SQL optimization of optimization
杰理之蓝牙耳机品控和生产技巧【篇】
Talking from mlperf: how to lead the next wave of AI accelerator
ngnix基础知识
Classify boost libraries by function
随机推荐
九章云极DataCanvas公司蝉联中国机器学习平台市场TOP 3
matlab遍历图像、字符串数组等基本操作
GaussDB(DWS)主动预防排查
基于YOLOv5的口罩佩戴检测方法
基于三维GIS的不动产管理应用
物联网rfid等
十三届蓝桥杯B组国赛
Fundamentals - IO intensive computing and CPU intensive computing
杰理之烧录都使用 VBAT 供电,供电电压 4.2V【篇】
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
分离字符串中的字母和数字并使得字母在前数组在后
PMP证书真的有用吗?
[deep learning] use deep learning to monitor your girlfriend's wechat chat?
Flume面试题
NIO与传统IO的区别
Classify boost libraries by function
Tops, the unit of computing power of the processor, can be carried out 1 trillion times per second
php反射型xss,反射型XSS测试及修复
基于K-means的用户画像聚类模型