当前位置:网站首页>多线程之锁
多线程之锁
2022-07-31 17:25:00 【51CTO】

线程锁分两种:内置锁(synchronized)和显式锁(ReentrantLock)
简单对比:
主要相同点:Lock能完成synchronized所实现的所有功能
主要不同点:Lock有比synchronized更精确的线程语义和更好的性能,当许多线程都在争用同一个锁时,使用 ReentrantLock 的总体开支通常要比 synchronized 少得多。
synchronized会自动释放锁,而Lock一定要求程序员手工释放,为了防止忘记释放锁,一般在finally从句中释放。
Synchronized不能设置等待时间,所以如果等待的资源一直被占用,可能会出现死锁.
ReentrantLock会灵活一点,可以设置等待的时间,如果等待的时间到了,就会去做其它的任务.


更多了解
边栏推荐
- 多数据中心操作和检测并发写入
- 迁移学习——Domain Adaptation
- Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
- 淘宝/天猫获得淘口令真实url API
- How to install CV2 smoothly in Anaconda
- 京东获取商品历史价格信息 API
- 【luogu P8326】Fliper(图论)(构造)(欧拉回路)
- Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
- 保证接口数据安全的10种方式
- 【Yugong Series】July 2022 Go Teaching Course 021-Slicing Operation of Go Containers
猜你喜欢
随机推荐
Jiuqi ny3p series voice chip replaces the domestic solution KT148A, which is more cost-effective and has a length of 420 seconds
Golang 小数操作之判断几位小数点与四舍五入
MySQL---排序与分页
21.支持向量机—核函数的介绍
华为顶级工程师历时9年总结的“趣谈网络协议”PDF文档,太强了
牛客网刷题(一)
flyway的快速入门教程
[pytorch] 1.7 pytorch and numpy, tensor and array conversion
无主复制系统(2)-读写quorum
MySQL---多表查询
flutter设置statusbar状态栏的背景颜色和 APP(AppBar)内部颜色一致方法。
[Source code analysis] BeanFactory and FactoryBean
Intelligent bin (9) - vibration sensor (raspberries pie pico implementation)
MySQL---聚合函数
Huawei's top engineers lasted nine years "anecdotal stories network protocol" PDF document summary, is too strong
【码蹄集新手村600题】通向公式与程序相结合
【pytorch】pytorch 自动求导、 Tensor 与 Autograd
[TypeScript] OOP
Masterless Replication System (3)-Limitations of Quorum Consistency
最后写入胜利(丢弃并发写入)









