当前位置:网站首页>The difference between synchronized and lock
The difference between synchronized and lock
2022-07-04 06:57:00 【No trace after wind 2018】
Synchronized and Lock The difference between
First synchronized yes java Built in keywords ,Lock It's a java Interface ;
synchronized Unable to determine whether to acquire the lock state ,Lock It can be judged whether the lock is obtained (trylock);
synchronized Will automatically release the lock ( The lock will be released after the synchronization code is executed or an exception occurs ),Lock Need to be in finally Release the lock by hand (unlock() Method release lock ), Otherwise, it is easy to cause thread deadlock ;
use synchronized Two threads of keyword 1 And thread 2, If the current thread 1 Gets the lock , Threads 2 Thread waiting . If the thread 1 Blocking , Threads 2 Then I will wait all the time , and Lock The lock doesn't have to wait , If an attempt to acquire a lock fails , Threads can end without waiting all the time ;
synchronized The lock of can be re entered 、 Non interruptible 、 Unfair , and Lock The lock can be reentered 、 interruptible (interrupt)、 Fair enough ( Either way )
Lock Lock is suitable for a large number of synchronized code synchronization problems ,synchronized Locks are suitable for synchronization problems with a small amount of code .
边栏推荐
- Research on an endogenous data security interaction protocol oriented to dual platform and dual chain architecture
- Responsive - media query
- Modify TCP timestamp to optimize transmission performance
- Wechat applet scroll view component scrollable view area
- MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0
- Another company raised the price of SAIC Roewe new energy products from March 1
- 【问题记录】03 连接MySQL数据库提示:1040 Too many connections
- Paddleocr prompt error: can not import AVX core while this file exists: xxx\paddle\fluid\core_ avx
- Tar source code analysis 8
- What is the sheji principle?
猜你喜欢

Mysql 45讲学习笔记(七)行锁

MySQL relearn 2- Alibaba cloud server CentOS installation mysql8.0

NLP-文献阅读总结

selenium驱动IE常见问题解决Message: Currently focused window has been closed.
![[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)](/img/03/2b37e63d0d482d5020b7421ac974cb.jpg)
[MySQL] introduction, function, creation, view, deletion and modification of database view (with exercises)

Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction

What is industrial computer encryption and how to do it

List of top ten professional skills required for data science work

Crawler (III) crawling house prices in Tianjin
![[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph](/img/5e/7ce21dd544aacf23b4ceef1ec547fd.png)
[GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
随机推荐
How to share the source code anti disclosure scheme
【FPGA教程案例7】基于verilog的计数器设计与实现
How notepad++ counts words
Selection (021) - what is the output of the following code?
Uniapp applet subcontracting
Explain in one sentence what social proof is
MySQL 45 lecture learning notes (12) MySQL will "shake" for a while
Tar source code analysis 6
测试用例的设计
P26-P34 third_ template
MySQL 45 learning notes (XI) how to index string fields
Finishing (III) - Exercise 2
The important role of host reinforcement concept in medical industry
BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
Can the out of sequence message complete TCP three handshakes
Selection (022) - what is the output of the following code?
Why does the producer / consumer mode wait () use while instead of if (clear and understandable)
NLP-文献阅读总结
tars源码分析之7
About how idea sets up shortcut key sets