当前位置:网站首页>读写锁、共享锁、独占锁
读写锁、共享锁、独占锁
2022-07-24 08:51:00 【linsa_pursuer】
乐观锁:修改的时候去判断
悲观锁:操作之前上锁
表锁:不会出现死锁,发生锁冲突几率高,并发低。
行锁:会出现死锁,发生锁冲突几率低,并发高。
读锁:共享锁,发生死锁
写锁:独占锁,发生死锁
读写锁:一个资源可以被多个读线程访问,或者可以被一个写线程访问,但是不能同时存在读写线程,读写互斥
第一 无锁
多线程抢夺资源 乱
第二 添加锁
使用synchronized和ReentrantLock
都是独占的,每次只能来一个操作
第三 读写锁
ReentrantReadWriteLock
读读 可以共享,提升性能,同时多人进行读操作
缺点:
(1)造成锁饥饿,一直读,没有写操作
(2)读时候,不能写,只有读完成之后,才可以写,写操作可以读
锁降级:将写入锁降级为读锁,读锁不能升级为写锁
jdk8说明:
写锁 降级 读锁
获取写锁——》获取读锁——》释放写锁——》释放读锁
增加修改删除——写
查询——读
锁降级示例
package juc;
import java.util.concurrent.locks.ReentrantReadWriteLock;
public class Demo1 {
public static void main(String[] args) {
ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
ReentrantReadWriteLock.ReadLock readLock = rwLock.readLock();//读锁
ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();//写锁
//锁降级
//1 获取写锁
writeLock.lock();
System.out.println("Linsa");
//2 获取读锁
readLock.lock();
System.out.println("read");
//3 释放写锁
writeLock.unlock();
//4 释放读锁
readLock.unlock();
}
}
边栏推荐
- Houdini official HDA sidefx labs installation
- Opencv Chinese document 4.0.0 learning notes (updating...)
- Notify consumers after provider information changes in RPC
- Move protocol launched a beta version, and you can "0" participate in p2e
- 利用opencv 做一个简单的人脸识别
- Scatter chart - date
- Change of sheetname
- 【FFH】OpenHarmony啃论文成长计划---cJSON在传统C/S模型下的应用
- [FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
- 【一起上水硕系列】June总结+no 焦虑+July计划+如何考试+如何提升
猜你喜欢

Basic use of Nacos (2) -- Nacos configuration center

3587. Connected graph (Jilin University postgraduate entrance examination machine test question)

C language practice questions + Answers:

Scatter chart - date

Porting boa server on imx6ull

Hack the box - Introduction to networking module detailed Chinese tutorial

超全总结:Go语言如何操作文件

Using OpenCV to do a simple face recognition

WordPress free theme: document, making reading more convenient

Take out the string in brackets
随机推荐
The beta version of move protocol is stable, and it is temporarily decided to expand the scale of the prize pool
Oauth2 server set up oauth2 authentication service
利用opencv 做一个简单的人脸识别
Porting boa server on imx6ull
From starfish OS' continued deflationary consumption of SFO, the value of SFO in the long run
Use the bark app to realize the process of pushing messages to mobile phones
剑指 Offer II 024. 反转链表
Will Plato become the risk target of the meta universe? Platofarm has great opportunities
[Shangshui Shuo series together] June summary +no anxiety +july plan + how to test + how to improve
Wargames NATAS (0-10) problem solving essay
JS built-in method
How RPC callers implement asynchronous calls: completable future
"Problem solving" Batman's trouble
Redis learning - Introduction to redis and NiO principles
Usage of volatile keyword in C language
安装软件时提示【An error occurred while trying to create a file in the destination directory: 拒绝访问】的解决方法
Bit.store, which has attracted much attention, is at a glance of the latest developments
Using golang to implement RPC (1)
看了纪录片《埃达克岛的海盗宝藏》,想到我国历史上的遗失秘宝
[FFH] websocket practice of real-time chat room