当前位置:网站首页>synchronized 和 ReentrantLock
synchronized 和 ReentrantLock
2022-07-06 00:33:00 【InfoQ】
synchronized 和 ReentrantLock 有什么区别?
- synchronized 和 ReentrantLock 都提供了锁的功能
- ReentrantLock 可设置为公平锁,而 synchronized 不能
- ReentrantLock 只能修饰代码块,而 synchronized 可以用于修饰方法、修饰代码块等;
- ReentrantLock 需要手动加锁和释放锁,锁没有释放,则会造成资源被占用一直得不到释放,而 synchronized 无需手动释放锁,由jvm内部自动加锁和释放锁
- ReentrantLock 可以主动的获取是否成功获得了锁的信息,而 synchronized 不行,可以理解为黑盒方法
使用
Lock lock = new ReentrantLock();
try {
lock.lock();
} finally {
lock.unlock();
}
- 直接修饰在方法声明上
- 声明在方法块中
原理的区别
- 每个对象有一个锁monitor,当monitor被占用时就会处于锁定状态
- 如果monitor的进入数为0,则该线程进入monitor,然后将进入数设置为1,该线程即为monitor的所有者
- 如果monitor的进入数不为0,则表示已有线程进入monitor,则该线程进入阻塞状态
- 线程执行完成后,退出monitor,将进入数设置为0
- CAS:Compare and Swap,比较并交换,此操作为一个原子操作
- CLH队列:带头结点的双向非循环链表
边栏推荐
- Permission problem: source bash_ profile permission denied
- 剖面测量之提取剖面数据
- [Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
- STM32 configuration after chip replacement and possible errors
- [Online gadgets] a collection of online gadgets that will be used in the development process
- Ffmpeg captures RTSP images for image analysis
- Huawei equipment is configured with OSPF and BFD linkage
- 7.5 simulation summary
- 如何解决ecology9.0执行导入流程流程产生的问题
- Global and Chinese markets of universal milling machines 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
LeetCode 1189. Maximum number of "balloons"
MySql——CRUD
Key structure of ffmpeg - avformatcontext
Comment faire votre propre robot
Hardware and interface learning summary
建立时间和保持时间的模型分析
Leetcode:20220213 week race (less bugs, top 10% 555)
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
Model analysis of establishment time and holding time
How to use the flutter framework to develop and run small programs
随机推荐
How spark gets columns in dataframe --column, $, column, apply
AtCoder Beginner Contest 254【VP记录】
2022-02-13 work record -- PHP parsing rich text
Configuring OSPF load sharing for Huawei devices
Go learning --- structure to map[string]interface{}
Room cannot create an SQLite connection to verify the queries
7.5 decorator
[noi simulation] Anaid's tree (Mobius inversion, exponential generating function, Ehrlich sieve, virtual tree)
The relationship between FPGA internal hardware structure and code
Leetcode 450 deleting nodes in a binary search tree
SQLServer连接数据库读取中文乱码问题解决
Global and Chinese markets for pressure and temperature sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Leetcode:20220213 week race (less bugs, top 10% 555)
JS can really prohibit constant modification this time!
Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
Analysis of the combination of small program technology advantages and industrial Internet
Spark DF adds a column
云导DNS和知识科普以及课堂笔记
[EI conference sharing] the Third International Conference on intelligent manufacturing and automation frontier in 2022 (cfima 2022)
Go learning - dependency injection