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

相关推荐
txinyu的博客10 小时前
HTTP服务实现用户级窗口限流
开发语言·c++·分布式·网络协议·http
独自破碎E10 小时前
RabbitMQ中的Prefetch参数
分布式·rabbitmq
深蓝电商API11 小时前
Scrapy+Rredis实现分布式爬虫入门与优化
分布式·爬虫·scrapy
回家路上绕了弯12 小时前
定期归档历史数据实战指南:从方案设计到落地优化
分布式·后端
rchmin13 小时前
Distro与Raft协议对比分析
分布式·cap
小辉笔记13 小时前
kafka原理总结
分布式·kafka
实战项目13 小时前
分布式协作入侵检测系统的报警信息管理
分布式
无心水16 小时前
【分布式利器:腾讯TSF】10、TSF故障排查与架构评审实战:Java架构师从救火到防火的生产哲学
java·人工智能·分布式·架构·限流·分布式利器·腾讯tsf
小北方城市网1 天前
分布式锁实战指南:从选型到落地,避开 90% 的坑
java·数据库·redis·分布式·python·缓存
范桂飓1 天前
大模型分布式训练框架 Megatron-LM
人工智能·分布式