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

相关推荐
Blossom.1185 小时前
基于Embedding+图神经网络的开源软件供应链漏洞检测:从SBOM到自动修复的完整实践
人工智能·分布式·深度学习·神经网络·copilot·开源软件·embedding
song50110 小时前
鸿蒙 Flutter 图像识别进阶:物体分类与花卉识别(含离线模型)
人工智能·分布式·python·flutter·3d·华为·分类
西格电力科技12 小时前
源网荷储与碳中和:推动能源清洁转型的关键路径
大数据·人工智能·分布式·系统架构·能源
-Xie-12 小时前
Redis(十四)——分布式锁
数据库·redis·分布式
武子康12 小时前
Java-190 EVCache入门:Netflix 级分布式缓存架构、性能指标与多区域部署全解析
java·redis·分布式·缓存·架构·guava·guava cache
5008413 小时前
鸿蒙 Flutter 分布式硬件调用:跨设备摄像头 / 麦克风共享
分布式·flutter·华为·electron·wpf·开源鸿蒙
Jerry9527062814 小时前
1.什么式可用性
java·分布式·后端·架构·高可用·秒杀
醉风塘16 小时前
RabbitMQ状态与配置深度解读:构建高性能消息中间件的关键指标
分布式·rabbitmq
赵得C17 小时前
软件设计师前沿考点精讲:新兴技术与性能优化实战
java·开发语言·分布式·算法·设计模式·性能优化
摇滚侠17 小时前
分布式锁,etcd,redis,ZooKeeper
redis·分布式·etcd