当前位置:网站首页>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
边栏推荐
- STM32 key chattering elimination - entry state machine thinking
- About the slmgr command
- Opencv classic 100 questions
- An understanding of & array names
- 2022-02-13 work record -- PHP parsing rich text
- 如何制作自己的機器人
- [Online gadgets] a collection of online gadgets that will be used in the development process
- Global and Chinese market of valve institutions 2022-2028: Research Report on technology, participants, trends, market size and share
- Leetcode 450 deleting nodes in a binary search tree
- 多线程与高并发(8)—— 从CountDownLatch总结AQS共享锁(三周年打卡)
猜你喜欢

Room cannot create an SQLite connection to verify the queries

Hudi of data Lake (1): introduction to Hudi

uniapp开发,打包成H5部署到服务器

Folding and sinking sand -- weekly record of ETF

KDD 2022 | 脑电AI助力癫痫疾病诊断

Idea远程提交spark任务到yarn集群

从 1.5 开始搭建一个微服务框架——调用链追踪 traceId

notepad++正則錶達式替換字符串

如何利用Flutter框架开发运行小程序

How to make your own robot
随机推荐
How to use the flutter framework to develop and run small programs
图解网络:TCP三次握手背后的原理,为啥两次握手不可以?
curlpost-php
Notepad + + regular expression replace String
如何制作自己的机器人
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
详细页返回列表保留原来滚动条所在位置
AtCoder Beginner Contest 258【比赛记录】
LeetCode 1189. Maximum number of "balloons"
OpenCV经典100题
PHP determines whether an array contains the value of another array
devkit入门
Pointer - character pointer
Introduction of motor
如何制作自己的機器人
How to make your own robot
[groovy] JSON string deserialization (use jsonslurper to deserialize JSON strings | construct related classes according to the map set)
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Cloud guide DNS, knowledge popularization and classroom notes