当前位置:网站首页>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
边栏推荐
- The relationship between FPGA internal hardware structure and code
- LeetCode 斐波那契序列
- Power Query数据格式的转换、拆分合并提取、删除重复项、删除错误、转置与反转、透视和逆透视
- 《编程之美》读书笔记
- Ffmpeg learning - core module
- Multithreading and high concurrency (8) -- summarize AQS shared lock from countdownlatch (punch in for the third anniversary)
- Common API classes and exception systems
- Ffmpeg captures RTSP images for image analysis
- STM32按键消抖——入门状态机思维
- [groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)
猜你喜欢
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
Extension and application of timestamp
Ffmpeg captures RTSP images for image analysis
Idea远程提交spark任务到yarn集群
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
数据分析思维分析方法和业务知识——分析方法(三)
How to solve the problems caused by the import process of ecology9.0
Calculate sha256 value of data or file based on crypto++
MySQL存储引擎
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
随机推荐
uniapp开发,打包成H5部署到服务器
devkit入门
如何制作自己的機器人
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
Spark SQL null value, Nan judgment and processing
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Key structure of ffmpeg - avframe
Notepad++ regular expression replacement string
XML配置文件
Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
常用API类及异常体系
KDD 2022 | 脑电AI助力癫痫疾病诊断
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
Hudi of data Lake (1): introduction to Hudi
OpenCV经典100题
Codeforces Round #804 (Div. 2)【比赛记录】
Arduino hexapod robot
如何解决ecology9.0执行导入流程流程产生的问题