日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

redisson分布式鎖中waittime的設置

作者:蔚藍色的風暴 更新時間: 2024-07-18 編程語言

之前分布式鎖中使用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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新