当前位置:网站首页>Redismission source code analysis
Redismission source code analysis
2022-07-08 01:51:00 【Know what you know】
Redisson There are two ways to lock ,tryLock and lock, The difference in use is tryLock You can set the expiration time of the lock leaseTime And waiting time waitTime, The logic of core processing is almost the same
public boolean tryLock(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException {
long time = unit.toMillis(waitTime);
long current = System.currentTimeMillis();
long threadId = Thread.currentThread().getId();
// Attempt to acquire lock , If you don't get the lock , The remaining timeout of the lock is returned
Long ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
// lock acquired
// ttl by null, It means we can get the lock , return true
if (ttl == null) {
return true;
}
// If waitTime It's over time , Just go back to false, Failed to apply for lock
time -= System.currentTimeMillis() - current;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
current = System.currentTimeMillis();
/**
Subscribe to lock release events , And pass await Method blocks waiting for the lock to be released , It effectively solves the problem of waste of resources in invalid lock application :
Based on the amount of information , When the lock is occupied by other resources , The current thread passes Redis Of channel Release event of subscription lock
Once the lock is released, a message will be sent to inform the waiting thread to compete .
*/
RFuture<RedissonLockEntry> subscribeFuture = subscribe(threadId);
if (!subscribeFuture.await(time, TimeUnit.MILLISECONDS)) {
if (!subscribeFuture.cancel(false)) {
subscribeFuture.onComplete((res, e) -> {
if (e == null) {
unsubscribe(subscribeFuture, threadId);
}
});
}
acquireFailed(waitTime, unit, threadId);
return false;
}
try {
// If the time taken to acquire the lock exceeds the maximum waiting time , Locking failed
time -= System.currentTimeMillis() - current;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
// Cycle to acquire the lock within the maximum waiting time
while (true) {
long currentTime = System.currentTimeMillis();
ttl = tryAcquire(waitTime, leaseTime, unit, threadId);
// lock acquired
if (ttl == null) {
return true;
}
time -= System.currentTimeMillis() - currentTime;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
// waiting for message
currentTime = System.currentTimeMillis();
// waiting for message, Wait for unlock message
if (ttl >= 0 && ttl < time) {
subscribeFuture.getNow().getLatch().tryAcquire(ttl, TimeUnit.MILLISECONDS);
} else {
subscribeFuture.getNow().getLatch().tryAcquire(time, TimeUnit.MILLISECONDS);
}
time -= System.currentTimeMillis() - currentTime;
if (time <= 0) {
acquireFailed(waitTime, unit, threadId);
return false;
}
}
} finally {
// Unsubscribe from messages
unsubscribe(subscribeFuture, threadId);
}
// return get(tryLockAsync(waitTime, leaseTime, unit));
}
Here's the thing to watch out for ,RedissonLock It's also not solved When the node hangs up , There is a risk of losing locks . And the reality is that there are some scenes that can't be tolerated , therefore Redisson Provides the implementation of redlock Algorithm RedissonRedLock,RedissonRedLock It really solves the problem of single point failure , The price is extra for RedissonRedLock build Redis Environmental Science .
therefore , If the business scenario can tolerate this small probability of error , It is recommended to use RedissonLock, If you can't stand , It is recommended to use RedissonRedLock.
边栏推荐
- Tencent game client development interview (unity + cocos) double bombing social recruitment 6 rounds of interviews
- cv2读取视频-并保存图像或视频
- adb工具介绍
- break net
- 批次管控如何实现?MES系统给您答案
- About snake equation (3)
- Anaconda3 tutorial on installing and adding Tsinghua image files
- Mysql database (2)
- Summary of log feature selection (based on Tianchi competition)
- Matlab method is good~
猜你喜欢

Android 创建的sqlite3数据存放位置

The function of carbon brush slip ring in generator

【目标跟踪】|DiMP: Learning Discriminative Model Prediction for Tracking

Why does the updated DNS record not take effect?

Version 2.0 of tapdata, the open source live data platform, has been released

快手小程序担保支付php源码封装

Redux使用

滑环在直驱电机转子的应用领域

ANSI / nema- mw- 1000-2020 magnetic iron wire standard Latest original

The foreach map in JS cannot jump out of the loop problem and whether foreach will modify the original array
随机推荐
子矩阵的和
《ClickHouse原理解析与应用实践》读书笔记(7)
Why did MySQL query not go to the index? This article will give you a comprehensive analysis
NPDP在国内有认可度吗?看一看就明白了!
城市土地利用分布数据/城市功能区划分布数据/城市poi感兴趣点/植被类型分布
qt--將程序打包--不要安裝qt-可以直接運行
Uniapp one click Copy function effect demo (finishing)
break net
How to realize batch control? MES system gives you the answer
Js中forEach map无法跳出循环问题以及forEach会不会修改原数组
COMSOL----微阻梁模型的搭建---最终的温度分布和变形情况----几何模型的建立
从Starfish OS持续对SFO的通缩消耗,长远看SFO的价值
Sum of submatrix
The usage of rand function in MATLAB
如何制作企业招聘二维码?
Sword finger offer II 041 Average value of sliding window
node js 保持长连接
从cmath文件看名字是怎样被添加到命名空间std中的
Redisson分布式锁解锁异常
The method of using thread in PowerBuilder