当前位置:网站首页>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 .
边栏推荐
- Download address of the official website of national economic industry classification gb/t 4754-2017
- MySQL 45 lecture learning notes (12) MySQL will "shake" for a while
- tars源码分析之9
- Tar source code analysis 4
- [GF (q) + LDPC] regular LDPC coding and decoding design and MATLAB simulation based on the GF (q) field of binary graph
- MySQL 45 lecture learning notes (x) force index
- tars源码分析之6
- What is the use of cloud redis? How to use cloud redis?
- JS common time processing functions
- 【MySQL】数据库视图的介绍、作用、创建、查看、删除和修改(附练习题)
猜你喜欢
Fundamentals of SQL database operation
用于压缩视频感知增强的多目标网络自适应时空融合
How notepad++ counts words
【GF(q)+LDPC】基于二值图GF(q)域的规则LDPC编译码设计与matlab仿真
移动适配:vw/vh
Recursive Fusion and Deformable Spatiotemporal Attention for Video Compression Artifact Reduction
BasicVSR++: Improving Video Super-Resolutionwith Enhanced Propagation and Alignment
[backpack DP] backpack problem
Four sets of APIs for queues
关于IDEA如何设置快捷键集
随机推荐
The important role of host reinforcement concept in medical industry
selenium IDE插件下载安装使用教程
2022 is probably the best year for the economy in the next 10 years. Did you graduate in 2022? What is the plan after graduation?
[number theory] fast power (Euler power)
tars源码分析之8
Tar source code analysis 8
tars源码分析之4
Redis面试题集
What is tweeman's law?
com. alibaba. nacos. api. exception. NacosException
2022, peut - être la meilleure année économique de la prochaine décennie, avez - vous obtenu votre diplôme en 2022? Comment est - ce prévu après la remise des diplômes?
P26-P34 third_ template
CORS is not intended to protect API endpoints - nikofischer
Tar source code analysis 9
Deep understanding of redis -- a new type of bitmap / hyperloglgo / Geo
关于IDEA如何设置快捷键集
[backpack DP] backpack problem
[network data transmission] FPGA based development of 100M / Gigabit UDP packet sending and receiving system, PC to FPGA
Option (024) - do all objects have prototypes?
2022年,或許是未來10年經濟最好的一年,2022年你畢業了嗎?畢業後是怎麼計劃的?