当前位置:网站首页>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
边栏推荐
- [groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
- Arduino六足机器人
- Atcoder beginer contest 254 [VP record]
- Global and Chinese market of water heater expansion tank 2022-2028: Research Report on technology, participants, trends, market size and share
- Location based mobile terminal network video exploration app system documents + foreign language translation and original text + guidance records (8 weeks) + PPT + review + project source code
- Keepalive component cache does not take effect
- The relationship between FPGA internal hardware structure and code
- 电机的简介
- [groovy] compile time meta programming (AST syntax tree conversion with annotations | define annotations and use groovyasttransformationclass to indicate ast conversion interface | ast conversion inte
- Set data real-time update during MDK debug
猜你喜欢

AtCoder Beginner Contest 254【VP记录】

Arduino六足机器人
![[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)](/img/e4/a41fe26efe389351780b322917d721.jpg)
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)

Arduino hexapod robot

Classical concurrency problem: the dining problem of philosophers

Opencv classic 100 questions

Set data real-time update during MDK debug

Room cannot create an SQLite connection to verify the queries
![[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)](/img/e4/a41fe26efe389351780b322917d721.jpg)
[groovy] compile time meta programming (compile time method interception | method interception in myasttransformation visit method)

Leetcode 450 deleting nodes in a binary search tree
随机推荐
[groovy] JSON serialization (jsonbuilder builder | generates JSON string with root node name | generates JSON string without root node name)
从 1.5 开始搭建一个微服务框架——调用链追踪 traceId
Pointer pointer array, array pointer
Classic CTF topic about FTP protocol
MySql——CRUD
MySQL storage engine
[designmode] composite mode
Browser reflow and redraw
Single source shortest path exercise (I)
uniapp开发,打包成H5部署到服务器
STM32按键消抖——入门状态机思维
【EI会议分享】2022年第三届智能制造与自动化前沿国际会议(CFIMA 2022)
Extension and application of timestamp
Spark获取DataFrame中列的方式--col,$,column,apply
Reading notes of the beauty of programming
curlpost-php
Key structure of ffmpeg - avframe
Go learning - dependency injection
Problems and solutions of converting date into specified string in date class
[groovy] JSON serialization (convert class objects to JSON strings | convert using jsonbuilder | convert using jsonoutput | format JSON strings for output)