当前位置:网站首页>The difference between synchronized and lock
The difference between synchronized and lock
2022-07-18 19:58:00 【Flowers fall in the rain】
1. Original composition
- synchronized Yes, the keyword belongs to JVM level .monitorenter( The bottom is through monitor Object complete , Actually wait/notify And so on monitor Objects can only be called in synchronous blocks or methods wait/notify Other methods ).
- Lock It's a concrete class (java.util.concurrent.locks.Lock) yes api Level lock
2. Usage method
- synchronized There is no need for the user to release the lock manually , When synchronized After the code is executed, the system will automatically let the thread release the lock occupation .
- ReentrantLock You need to release the lock manually. If there is no active release of the lock , It may lead to deadlock . need lock() and unlock() Method coordination try/finally Statement block to complete .
3. Whether waiting can be interrupted
- synchronized Non interruptible , Unless an exception is thrown or normal operation is completed .
- ReentrantLock interruptible , Method 1: Set supermarket method tryLock(long timeout, TimeUnit unit), Method 2:lockInterruptibly() Put it in the code block , call interrupt() Method can be interrupted
4. Is it fair to lock
- synchronized Not fair lock
- ReentrantLock Either way , Default fair lock , The constructor can pass in boolean value ,true Lock for fairness ,false Lock for unfairness
5. Lock binding multiple fields Condition
- synchronized No,
- ReentrantlLock It is used to wake up the threads that need to wake up in groups , Can wake up exactly , Not like it synchronized Either wake up one thread randomly or wake up all threads .
边栏推荐
- Android day 26: database two
- Vivado ROM IP core
- 串相关代码题--C语言
- jupyter notebook的kernel管理
- Get the activity return parameter
- Zkapp transaction snark in Mina
- JS array element duplication check
- Share constantly, tread the waves | developers say · dtalk mid year appreciation
- 11.滑动窗口的最大值——重要结构双端队列
- Raspberry pie shutdown restart command
猜你喜欢

Vivado ROM IP core

5.线程分离

Ci521 domestic 13.56MHz reader chip replaces cv520 compatible

马斯克76岁父亲与继女生子,华强北又出一个芯片IPO,原蚂蚁副总投身AI制药,今日更多大新闻在此...

Payment transaction snark in Mina

Liu tiemeng's introduction to C language - detailed explanation of entrustment

Musk's 76 year old father and stepdaughter have children. Huaqiangbei has another chip IPO. The former vice president of ant has joined AI pharmaceutical. Today, more big news is here

LeetCode第82场双周赛

【深度学习】线上租用设备平台体验以及踩过的坑(非广告)

SI24R2E_ Smart electronic student card 2.4GHz attendance scheme chip
随机推荐
Android day 26: database two
leetcode 8. String to Integer (atoi)(string)
Use of unity dotween plug-in and itween plug-in (notes)
NC20583 [SDOI2016]齿轮
安卓 Day 27 :数据库One
1. Threads and processes
11.滑动窗口的最大值——重要结构双端队列
Horizon 8 测试环境部署(7): App Volumes 部署
Horizon 8 测试环境部署(8): App Volumes Managers 负载均衡配置
Unsafe service permissions cooperate with MSF to raise rights
[200 opencv routines] 232 Spectral method of feature description
Horizon 8 测试环境部署(6): UAG 负载均衡配置-2
基于FPGA的内部IP核fifo信号仿真
Custom view
Five string high-frequency interview questions, grasp the underlying principle of string!
[200 routines OpenCV] 232. Méthode spectrale de caractérisation
【剑指 Offer II 041. 滑动窗口的平均值】
Get the activity return parameter
Unity typewriter effect timer implementation and dotween implementation (notes)
jupyter notebook的kernel管理