網站首頁 編程語言 正文
之前分布式鎖中使用redisson的lock.tryLock(0,0,TimeUnit.SECONDS)
這么做的邏輯是releaseTime設置為0,實際上會使用默認的30s并觸發看門狗機制
那waitTime有沒有默認值呢?設置為0實際會等待多少時間?
看源碼
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();
Long ttl = this.tryAcquire(waitTime, leaseTime, unit, threadId);
if (ttl == null) {
return true;
} else {
time -= System.currentTimeMillis() - current;
if (time <= 0L) {
this.acquireFailed(waitTime, unit, threadId);
return false;
} else {
........
這里的time直接就取的傳入的waitTime,當time減少到小于0時,返回加鎖失敗!
所以waitTime是沒有什么默認值的,這么寫相當于加鎖失敗立刻返回
實驗一下,先加一個不會過期的鎖,然后另一個線程試圖獲取鎖
@Test
public void getLock() throws InterruptedException {
String lockKey = "testLock";
RLock lock = redissonClient.getLock(lockKey);
System.out.println(System.currentTimeMillis());
Boolean isLock = lock.tryLock(0,-1,TimeUnit.SECONDS);
System.out.println(System.currentTimeMillis());
System.out.println(isLock);
}
如果設置為0,在嘗試獲取不到鎖后立刻就會返回失敗
原文鏈接:https://blog.csdn.net/qq_37831759/article/details/138490511
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2022-08-18 Kotlin函數使用示例教程_Android
- 2022-07-12 for循環中var和let的不為人知的秘密
- 2022-02-18 yml配置文件中存在@無法識別,報錯:found character ‘@‘ that cannot
- 2023-04-03 Python之parser.add_argument解讀_python
- 2022-03-27 MongoDB4.28開啟權限認證配置用戶密碼登錄功能_MongoDB
- 2022-05-10 Element-ui 中 Table 表格的設置表頭/去除下標線/設置行間距等屬性的使用及 slot
- 2022-08-26 Input標簽自動校驗功能去除實現_React
- 2022-08-01 GO項目配置與使用的方法步驟_Golang
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支