失败无限重试- 多个线程竞争,第一个拿到锁第二个会无限重试
RLock lock = redisson.getLock("码哥字节");
try {
lock.lock();
.....
} finally {
lock.unlock();
}
拿锁失败时会不停的重试,具有Watch Dog 自动延期机制,默认续30s 每隔30/3=10 秒续到30s。
watchDog 只有在未显示指定加锁超时时间(leaseTime)时才会生效。
失败超时重试,自动续命
boolean flag = lock.tryLock(10, TimeUnit.SECONDS);
超时自动释放锁
lock.lock(10, TimeUnit.SECONDS);
超时重试,自动解锁
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
lock.unlock();
}
}
具体使用方法详见下面代码
public void lock() throws InterruptedException{
log.info("线程:{},进入方法",Thread.currentThread().getName());
RLock rLock = redissonClient.getLock("lock");
rLock.lock();
long timeToLive = rLock.remainTimeToLive();
log.info("线程:{},获得锁,锁存活时间:{}S",Thread.currentThread().getName(),timeToLive/1000);
Thread.sleep(2000);
rLock.unlock();
log.info("线程:{},释放锁",Thread.currentThread().getName());
}
public void lockLaseTime() throws InterruptedException{
log.info("线程:{},进入方法",Thread.currentThread().getName());
RLock rLock = redissonClient.getLock("lockLaseTime");
rLock.lock(10,TimeUnit.SECONDS);
long timeToLive = rLock.remainTimeToLive();
log.info("线程:{},获得锁,锁存活时间:{}S",Thread.currentThread().getName(),timeToLive/1000);
Thread.sleep(2000);
rLock.unlock();
log.info("线程:{},释放锁",Thread.currentThread().getName());
}
public void tryLock() throws InterruptedException {
log.info("线程:{},进入方法",Thread.currentThread().getName());
RLock rLock = redissonClient.getLock("tryLock");
boolean flag = rLock.tryLock();
if (flag){
long timeToLive = rLock.remainTimeToLive();
log.info("线程:{},获得锁,锁存活时间:{}S,加锁状态:{}",Thread.currentThread().getName(),timeToLive/1000,flag);
Thread.sleep(2000);
rLock.unlock();
log.info("线程:{},释放锁",Thread.currentThread().getName());
}else {
log.info("线程:{},获得锁失败",Thread.currentThread().getName());
}
}
public void tryLockWaitTime() throws InterruptedException {
log.info("线程:{},进入方法",Thread.currentThread().getName());
RLock rLock = redissonClient.getLock("tryLockWaitTime");
boolean flag = rLock.tryLock(6, TimeUnit.SECONDS);
if (flag){
long timeToLive = rLock.remainTimeToLive();
log.info("线程:{},获得锁,锁存活时间:{}S,加锁状态:{}",Thread.currentThread().getName(),timeToLive/1000,flag);
Thread.sleep(10000);
rLock.unlock();
log.info("线程:{},释放锁",Thread.currentThread().getName());
}else {
log.info("线程:{},获得锁失败",Thread.currentThread().getName());
}
}
public void tryLockleasTime() throws InterruptedException {
log.info("线程:{},进入方法",Thread.currentThread().getName());
RLock rLock = redissonClient.getLock("tryLockleasTime");
boolean flag = rLock.tryLock(11,10, TimeUnit.SECONDS);
if (flag){
long timeToLive = rLock.remainTimeToLive();
log.info("线程:{},获得锁,锁存活时间:{}S,加锁状态:{}",Thread.currentThread().getName(),timeToLive/1000,flag);
Thread.sleep(6000);
rLock.unlock();
log.info("线程:{},释放锁",Thread.currentThread().getName());
}else {
log.info("线程:{},获得锁失败",Thread.currentThread().getName());
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析