当前位置:网站首页>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)
- About the slmgr command
- curlpost-php
- Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
- synchronized 和 ReentrantLock
- Cloud guide DNS, knowledge popularization and classroom notes
- 数据分析思维分析方法和业务知识——分析方法(二)
- PHP determines whether an array contains the value of another array
- [designmode] Decorator Pattern
- Notepad + + regular expression replace String
猜你喜欢
Calculate sha256 value of data or file based on crypto++
[groovy] XML serialization (use markupbuilder to generate XML data | set XML tag content | set XML tag attributes)
Atcoder beginer contest 258 [competition record]
FPGA内部硬件结构与代码的关系
Uniapp development, packaged as H5 and deployed to the server
[groovy] XML serialization (use markupbuilder to generate XML data | create sub tags under tag closures | use markupbuilderhelper to add XML comments)
如何利用Flutter框架开发运行小程序
Model analysis of establishment time and holding time
Recognize the small experiment of extracting and displaying Mel spectrum (observe the difference between different y_axis and x_axis)
Keepalive component cache does not take effect
随机推荐
Pointer - character pointer
uniapp开发,打包成H5部署到服务器
PHP determines whether an array contains the value of another array
详细页返回列表保留原来滚动条所在位置
Hudi of data Lake (2): Hudi compilation
Leetcode:20220213 week race (less bugs, top 10% 555)
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
MySql——CRUD
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
Browser reflow and redraw
Common API classes and exception systems
CTF daily question day44 rot
免费的聊天机器人API
Spark AQE
[simple implementation of file IO]
LeetCode 6004. Get operands of 0
NLP text processing: lemma [English] [put the deformation of various types of words into one form] [wet- > go; are- > be]
[Chongqing Guangdong education] reference materials for Zhengzhou Vocational College of finance, taxation and finance to play around the E-era
Meta AI西雅图研究负责人Luke Zettlemoyer | 万亿参数后,大模型会持续增长吗?
Codeforces Round #804 (Div. 2)【比赛记录】