当前位置:网站首页>[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
边栏推荐
- Using securecrtportable to remotely connect virtual machines
- 转--深入LUA脚本语言,让你彻底明白调试原理
- Kubernetes create service access pod
- [daily training] 326 Power of 3
- Explain ThreadLocal in detail
- Fiori 应用通过 Adaptation Project 的增强方式分享
- Origin2018 installation tutorial "recommended collection"
- MySQL -- deduction of index storage model
- Stimulate new kinetic energy and promote digital economy in multiple places
- Share some feelings of a programmer who has experienced layoffs twice a year
猜你喜欢
vSphere+、vSAN+来了!VMware 混合云聚焦:原生、快速迁移、混合负载
Selection of all-optical technology in the park - Part 2
Vsphere+ and vsan+ are coming! VMware hybrid cloud focus: native, fast migration, mixed load
Rust语言——小小白的入门学习05
元宇宙可能成为互联网发展的新方向
思科考试--冗余网络
Ffmpeg learning notes
转--深入LUA脚本语言,让你彻底明白调试原理
SAP ui5 application development tutorial 104 - multi select support for SAP ui5 table controls and how to use code to select multiple table row items at a time
14年本科毕业,3个月转行软件测试月薪13.5k,32的岁我终于找对了方向
随机推荐
Rust language - Introduction to Xiaobai 05
ECMAScript 2022 正式发布,有你了解过的吗?
Demo program implementation of QT version Huarui camera
Reprint CSDN article operation
A few minutes before work, I found out V-model and The difference between sync
Delete AWS bound credit card account
SAP intelligent robot process automation (IRPA) solution sharing
台积电全球员工薪酬中位数约46万,CEO约899万;苹果上调日本的 iPhone 售价 ;Vim 9.0 发布|极客头条
Happy number [fast and slow pointer of ring PROBLEMS]
Deadlock handling strategies - prevent deadlock, avoid deadlock, detect and remove deadlock
MySQL中对于事务的理解
447 Bili Bili noodles warp 1
陈天奇的机器学习编译课(免费)
What class loading mechanisms does the JVM have?
转--原来gdb的底层调试原理这么简单
MySQL -- index of MyISAM storage engine
internal field separator
mixconv代码
tcpdump命令使用详解
友善串口助手使用教程_友善串口调试助手怎么进行配置-友善串口调试助手使用教程…