当前位置:网站首页>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队列:带头结点的双向非循环链表
边栏推荐
- Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
- Single source shortest path exercise (I)
- Spark DF增加一列
- Folding and sinking sand -- weekly record of ETF
- Gavin teacher's perception of transformer live class - rasa project actual combat e-commerce retail customer service intelligent business dialogue robot system behavior analysis and project summary (4
- Configuring OSPF load sharing for Huawei devices
- Global and Chinese markets for hinged watertight doors 2022-2028: Research Report on technology, participants, trends, market size and share
- Uniapp development, packaged as H5 and deployed to the server
- Go learning - dependency injection
- Huawei equipment is configured with OSPF and BFD linkage
猜你喜欢
MySql——CRUD
Tools to improve work efficiency: the idea of SQL batch generation tools
Knowledge about the memory size occupied by the structure
Classic CTF topic about FTP protocol
Analysis of the combination of small program technology advantages and industrial Internet
Huawei equipment is configured with OSPF and BFD linkage
Arduino六足机器人
Room cannot create an SQLite connection to verify the queries
Configuring OSPF GR features for Huawei devices
Leetcode:20220213 week race (less bugs, top 10% 555)
随机推荐
数据分析思维分析方法和业务知识——分析方法(三)
Codeforces gr19 D (think more about why the first-hand value range is 100, JLS yyds)
Intranet Security Learning (V) -- domain horizontal: SPN & RDP & Cobalt strike
[Online gadgets] a collection of online gadgets that will be used in the development process
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
7.5 decorator
Priority queue (heap)
Search (DFS and BFS)
How to use the flutter framework to develop and run small programs
Key structure of ffmpeg - avframe
Spark DF adds a column
Basic introduction and source code analysis of webrtc threads
Codeforces Round #804 (Div. 2)【比赛记录】
Yolov5, pychar, Anaconda environment installation
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
Browser local storage
Classic CTF topic about FTP protocol
Spark获取DataFrame中列的方式--col,$,column,apply
Spark AQE
Knowledge about the memory size occupied by the structure