当前位置:网站首页>Read write lock, shared lock, exclusive lock
Read write lock, shared lock, exclusive lock
2022-07-24 08:53:00 【linsa_ pursuer】
Optimism lock : Judge when modifying
Pessimistic locking : Lock before operation
Table locks : A deadlock will not occur , The probability of lock conflict is high , Low concurrency .
Row lock : A deadlock occurs , Low probability of lock conflict , High concurrency .
Read the lock : Shared lock , There's a deadlock
Write lock : An exclusive lock , There's a deadlock
Read-write lock : A resource can be accessed by multiple read threads , Or it can be accessed by a write thread , But there can't be read and write threads at the same time , Reading and writing are mutually exclusive
First of all unlocked
Multithreading grabs resources The disorderly
second Add lock
Use synchronized and ReentrantLock
It's all exclusive , Only one operation at a time
Third Read-write lock
ReentrantReadWriteLock
Read Can be Shared , Lifting performance , Read by multiple people at the same time
shortcoming :
(1) Cause lock hunger , Read all the time , No write operation
(2) Reading time , Can't write , Only after reading , To write , Write operation can read
Lock down : Demote write lock to read lock , Read lock cannot be upgraded to write lock
jdk8 explain :
Write lock Downgrade Read the lock
Get write lock ——》 Get read lock ——》 Release the write lock ——》 Release read lock
Add modify delete —— Write
Inquire about —— read
Lock demotion example
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();// Read the lock
ReentrantReadWriteLock.WriteLock writeLock = rwLock.writeLock();// Write lock
// Lock down
//1 Get write lock
writeLock.lock();
System.out.println("Linsa");
//2 Get read lock
readLock.lock();
System.out.println("read");
//3 Release the write lock
writeLock.unlock();
//4 Release read lock
readLock.unlock();
}
}
边栏推荐
- Optimization of MySQL paging query
- 【FFH】实时聊天室之WebSocket实战
- [Shangshui Shuo series together] June summary +no anxiety +july plan + how to test + how to improve
- Interviewer: man, how much do you know about the read-write lock of go language?
- Kotlin学习笔记1——变量、函数
- 【一起上水硕系列】EE feedback 详解
- Houdini 官方HDA SideFX Labs 安装
- Using golang to implement RPC (1)
- Group by group and get the first data
- Valentine's Day
猜你喜欢

Virtual machine terminator terminal terminator installation tutorial

WordPress free theme: document, making reading more convenient

Shanghai issued a document to support the entry of NFT cultural exchange: the trend of digital collections has arrived!

How to use redis' publish subscribe (MQ) in.Netcore 3.1 project

Wargames NATAS (0-10) problem solving essay

使用Go语言开发eBPF程序
![[Sheung Shui Shuo series] EE feedback details](/img/19/5a80d5ef80d75be74b7ce08d6df058.png)
[Sheung Shui Shuo series] EE feedback details

Look at the most influential infrastructure m-dao of Web3 through the current situation of Dao

Houdini 官方HDA SideFX Labs 安装

Four data interaction modes of go grpc
随机推荐
Unity C#工具类 ArrayHelper
Unity C tool class arrayhelper
Sword finger offer II 024. reverse linked list
Typora提示【This beta version of Typora is expired, please download and install a newer version】的解决方案
Grpc learning notes
【FFH】OpenHarmony啃论文成长计划---cJSON在传统C/S模型下的应用
Online lover
Hack the box - File Inclusion module detailed Chinese tutorial
Move protocol launched a beta version, and you can "0" participate in p2e
Paclitaxel loaded tpgs reduced albumin nanoparticles /ga-hsa gambogic acid human serum protein nanoparticles
[FFH] websocket practice of real-time chat room
Super complete summary: how to operate files in go language
Overseas media, domestic we media, media publicity
Limited and unlimited Games: crypto
mysql URL
C # briefly describe the application of Richter's replacement principle
[emotion] what is "excellent"
Aquanee: the true meaning of "p2e"
Group by group and get the first data
Learn the rxjs operator