当前位置:网站首页>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
边栏推荐
- Anconda download + add Tsinghua +tensorflow installation +no module named 'tensorflow' +kernelrestart: restart failed, kernel restart failed
- Browser reflow and redraw
- Natural language processing (NLP) - third party Library (Toolkit):allenlp [library for building various NLP models; based on pytorch]
- [simple implementation of file IO]
- Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
- Priority queue (heap)
- LeetCode 8. String conversion integer (ATOI)
- How spark gets columns in dataframe --column, $, column, apply
- Pointer - character pointer
- Atcoder beginer contest 258 [competition record]
猜你喜欢
XML配置文件
Browser local storage
anconda下载+添加清华+tensorflow 安装+No module named ‘tensorflow‘+KernelRestarter: restart failed,内核重启失败
Calculate sha256 value of data or file based on crypto++
如何制作自己的机器人
免费的聊天机器人API
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
Start from the bottom structure and learn the introduction of fpga---fifo IP core and its key parameters
[groovy] compile time metaprogramming (compile time method injection | method injection using buildfromspec, buildfromstring, buildfromcode)
AtCoder Beginner Contest 258【比赛记录】
随机推荐
Leetcode 44 Wildcard matching (2022.02.13)
MySQL functions
Key structure of ffmpeg - avframe
Spark DF增加一列
Introduction of motor
MySql——CRUD
The global and Chinese markets of dial indicator calipers 2022-2028: Research Report on technology, participants, trends, market size and share
[Chongqing Guangdong education] Chongqing Engineering Vocational and Technical College
Ffmpeg captures RTSP images for image analysis
MySQL storage engine
详细页返回列表保留原来滚动条所在位置
Go learning --- structure to map[string]interface{}
How to make your own robot
MIT博士论文 | 使用神经符号学习的鲁棒可靠智能系统
Data analysis thinking analysis methods and business knowledge -- analysis methods (II)
OpenCV经典100题
devkit入门
Go learning --- read INI file
Key structure of ffmpeg -- AVCodecContext
如何制作自己的機器人