当前位置:网站首页>Synchronized and reentrantlock
Synchronized and reentrantlock
2022-07-06 00:37:00 【InfoQ】
synchronized and ReentrantLock What's the difference? ?
- synchronized and ReentrantLock Both provide the function of lock
- ReentrantLock Can be set to fair lock , and synchronized You can't
- ReentrantLock Only code blocks can be decorated , and synchronized Can be used to modify methods 、 Decorate code blocks, etc ;
- ReentrantLock You need to manually lock and release the lock , The lock is not released , It will cause resources to be occupied and not be released , and synchronized There is no need to release the lock manually , from jvm Internal automatic locking and release lock
- ReentrantLock You can actively obtain information about whether the lock has been successfully obtained , and synchronized no way , It can be understood as black box method
Use
Lock lock = new ReentrantLock();
try {
lock.lock();
} finally {
lock.unlock();
}
- Directly decorated on the method declaration
- Declared in a method block
The difference in principle
- Each object has a lock monitor, When monitor It will be locked when occupied
- If monitor The number of entries is 0, Then the thread enters monitor, Then set the number of entries to 1, This thread is monitor Owner
- If monitor The number of entries is not 0, Indicates that a thread has entered monitor, Then the thread enters the blocking state
- After thread execution , sign out monitor, Set the entry number to 0
- CAS:Compare and Swap, Compare and exchange , This operation is an atomic operation
- CLH queue : Two way acyclic linked list with leading node
边栏推荐
- Spark SQL空值Null,NaN判断和处理
- How to use the flutter framework to develop and run small programs
- KDD 2022 | 脑电AI助力癫痫疾病诊断
- LeetCode 8. String conversion integer (ATOI)
- NLP generation model 2017: Why are those in transformer
- AtCoder Beginner Contest 258【比赛记录】
- Spark DF adds a column
- Key structure of ffmpeg - avformatcontext
- OS i/o devices and device controllers
- Model analysis of establishment time and holding time
猜你喜欢
Ffmpeg learning - core module
剖面测量之提取剖面数据
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
看抖音直播Beyond演唱会有感
《强化学习周刊》第52期:Depth-CUPRL、DistSPECTRL & Double Deep Q-Network
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)
Set data real-time update during MDK debug
Folding and sinking sand -- weekly record of ETF
AtCoder Beginner Contest 254【VP记录】
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
随机推荐
Ffmpeg learning - core module
MYSQL GROUP_ The concat function realizes the content merging of the same ID
MySQL functions
Problems and solutions of converting date into specified string in date class
Mysql - CRUD
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
小程序技术优势与产业互联网相结合的分析
Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
FPGA内部硬件结构与代码的关系
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
STM32按键消抖——入门状态机思维
Cloud guide DNS, knowledge popularization and classroom notes
Comment faire votre propre robot
Arduino六足机器人
How to solve the problems caused by the import process of ecology9.0
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
如何解决ecology9.0执行导入流程流程产生的问题
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
Free chat robot API
Spark SQL UDF function