redisson分布式锁中waittime的设置

之前分布式锁中使用redisson的lock.tryLock(0,0,TimeUnit.SECONDS)

这么做的逻辑是releaseTime设置为0,实际上会使用默认的30s并触发看门狗机制

那waitTime有没有默认值呢?设置为0实际会等待多少时间?

看源码

java 复制代码
 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是没有什么默认值的,这么写相当于加锁失败立刻返回

实验一下,先加一个不会过期的锁,然后另一个线程试图获取锁

java 复制代码
@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,在尝试获取不到锁后立刻就会返回失败

相关推荐
初次攀爬者13 小时前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
断手当码农2 天前
Redis 实现分布式锁的三种方式
数据库·redis·分布式
初次攀爬者2 天前
Redis分布式锁实现的三种方式-基于setnx,lua脚本和Redisson
redis·分布式·后端
业精于勤_荒于稀2 天前
物流订单系统99.99%可用性全链路容灾体系落地操作手册
分布式
Asher05092 天前
Hadoop核心技术与实战指南
大数据·hadoop·分布式
凉凉的知识库2 天前
Go中的零值与空值,你搞懂了么?
分布式·面试·go
?Anita Zhang2 天前
联邦学习实战:如何在分布式场景下构建隐私保护机器学习模型
人工智能·分布式·机器学习
tony3652 天前
pytorch分布式训练解释
人工智能·pytorch·分布式
2501_933329552 天前
技术深度拆解:Infoseek媒体发布系统的分布式架构与自动化实现
分布式·架构·媒体
星辰_mya2 天前
消息队列遇到Producer发送慢
分布式·kafka