一、锁定
基于Redis的Java分布式可重入锁对象,并实现了锁接口。
如果获取锁的Redisson实例崩溃,则此类锁可能会在获取状态下永久挂起。为了避免这种Redisson维护锁watchdog,当锁持有者Redisson实例处于活动状态时,它会延长锁的到期时间。默认情况下,lock watchdog超时为30秒,可以通过Config.lockWatchdogTimeout设置进行更改。
可以定义锁获取期间的leaseTime参数。在指定的时间间隔后,锁定的锁将自动释放。
RLock对象的行为符合Java Lock规范。这意味着只有锁所有者线程才能解锁它,否则将引发IllegalMonitorStateException。否则,请考虑使用RSemaphore对象。
代码示例:
RLock lock = redisson.getLock("myLock");
// traditional lock method
lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
lock.unlock();
}
}
异步接口用法的代码示例:
RLock lock = redisson.getLock("myLock");
RFuture lockFuture = lock.lockAsync();
// or acquire lock and automatically unlock it after 10 seconds
RFuture lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
RFuture lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
lockFuture.whenComplete((res, exception) -> {
// ...
lock.unlockAsync();
});
反应式接口用法的代码示例:
RedissonReactiveClient redisson = redissonClient.reactive();
RLockReactive lock = redisson.getLock("myLock");
Mono lockMono = lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Mono lockMono = lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Mono lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);
lockMono.doOnNext(res -> {
// ...
})
.doFinally(lock.unlock())
.subscribe();
RxJava3 接口使用的代码示例:
RedissonRxClient redisson = redissonClient.rxJava();
RLockRx lock = redisson.getLock("myLock");
Completable lockRes = lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Completable lockRes = lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Single lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
lockRes.doOnSuccess(res -> {
// ...
})
.doFinally(lock.unlock())
.subscribe();
二、公平锁定
基于Redis的分布式可重入公平Java锁对象实现了锁接口。
公平锁保证线程将按照请求的顺序获取它。所有等待的线程都将排队,如果某个线程已死亡,Redisson将等待其返回5秒。例如,如果5个线程由于某种原因而死亡,那么延迟将为25秒。
如果获取锁的Redisson实例崩溃,则此类锁可能会在获取状态下永久挂起。为了避免这种Redisson维护锁看门狗,当锁持有者Redisson实例处于活动状态时,它会延长锁的到期时间。默认情况下,锁看门狗超时为30秒,可以通过Config.lockWatchdogTimeout设置进行更改。
可以定义锁获取期间的leaseTime参数。在指定的时间间隔后,锁定的锁将自动释放。
RLock对象的行为符合Java Lock规范。这意味着只有锁所有者线程才能解锁它,否则将引发IllegalMonitorStateException。否则,请考虑使用RSemaphore对象。
代码示例:
RLock lock = redisson.getFairLock("myLock");
// traditional lock method
lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
lock.unlock();
}
}
异步接口用法的代码示例:
RLock lock = redisson.getFairLock("myLock");
RFuture lockFuture = lock.lockAsync();
// or acquire lock and automatically unlock it after 10 seconds
RFuture lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
RFuture lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
lockFuture.whenComplete((res, exception) -> {
// ...
lock.unlockAsync();
});
Reactive接口 用法的代码示例:
RedissonReactiveClient redisson = redissonClient.reactive();
RLockReactive lock = redisson.getFairLock("myLock");
Mono lockMono = lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Mono lockMono = lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Mono lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);
lockMono.doOnNext(res -> {
// ...
})
.doFinally(lock.unlock())
.subscribe();
RxJava3 接口使用的代码示例:
RedissonRxClient redisson = redissonClient.rxJava();
RLockRx lock = redisson.getFairLock("myLock");
Completable lockRes = lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Completable lockRes = lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Single lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
lockRes.doOnSuccess(res -> {
// ...
})
.doFinally(lock.unlock())
.subscribe();
三、多重锁
基于Redis的分布式MultiLock对象允许将Lock对象分组,并将其作为单个锁处理。每个RLock对象可能属于不同的Redisson实例。
如果获取MultiLock的Redisson实例崩溃,那么这样的MultiLock可能会永远挂在获取状态。为了避免这种Redisson维护锁watchdog,当锁持有者Redisson实例处于活动状态时,它会延长锁的到期时间。默认情况下,lock watchdog超时为30秒,可以通过Config.lockWatchdogTimeout设置进行更改。
可以定义锁获取期间的leaseTime参数。在指定的时间间隔后,锁定的锁将自动释放。
MultiLock对象的行为符合Java Lock规范。这意味着只有锁所有者线程才能解锁它,否则将引发IllegalMonitorStateException。否则,请考虑使用RSemaphore对象。
代码示例:
RLock lock1 = redisson1.getLock("lock1");
RLock lock2 = redisson2.getLock("lock2");
RLock lock3 = redisson3.getLock("lock3");
RLock multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
// traditional lock method
multiLock.lock();
// or acquire lock and automatically unlock it after 10 seconds
multiLock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
boolean res = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
multiLock.unlock();
}
}
异步接口用法的代码示例:
RLock lock1 = redisson1.getLock("lock1");
RLock lock2 = redisson2.getLock("lock2");
RLock lock3 = redisson3.getLock("lock3");
RLock multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
RFuture lockFuture = multiLock.lockAsync();
// or acquire lock and automatically unlock it after 10 seconds
RFuture lockFuture = multiLock.lockAsync(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
RFuture lockFuture = multiLock.tryLockAsync(100, 10, TimeUnit.SECONDS);
lockFuture.whenComplete((res, exception) -> {
// ...
multiLock.unlockAsync();
});
反应式接口用法的代码示例:
RedissonReactiveClient anyRedisson = redissonClient.reactive();
RLockReactive lock1 = redisson1.getLock("lock1");
RLockReactive lock2 = redisson2.getLock("lock2");
RLockReactive lock3 = redisson3.getLock("lock3");
RLockReactive multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
Mono lockMono = multiLock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Mono lockMono = multiLock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Mono lockMono = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
lockMono.doOnNext(res -> {
// ...
})
.doFinally(multiLock.unlock())
.subscribe();
RxJava3 接口使用的代码示例:
RedissonRxClient anyRedisson = redissonClient.rxJava();
RLockRx lock1 = redisson1.getLock("lock1");
RLockRx lock2 = redisson2.getLock("lock2");
RLockRx lock3 = redisson3.getLock("lock3");
RLockRx multiLock = anyRedisson.getMultiLock(lock1, lock2, lock3);
Completable lockRes = multiLock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Completable lockRes = multiLock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Single lockRes = multiLock.tryLock(100, 10, TimeUnit.SECONDS);
lockRes.doOnSuccess(res -> {
// ...
})
.doFinally(multiLock.unlock())
.subscribe();
四、读写锁定
基于Redis的Java分布式可重入读写锁对象实现了读写锁接口。读和写锁都实现RLock接口。
只允许有多个ReadLock所有者和一个WriteLock所有者。
如果获取锁的Redisson实例崩溃,则此类锁可能会在获取状态下永久挂起。为了避免这种Redisson维护锁看门狗,当锁持有者Redisson实例处于活动状态时,它会延长锁的到期时间。默认情况下,锁看门狗超时为30秒,可以通过Config.lockWatchdogTimeout设置进行更改。
此外,Redisson允许在获取锁期间指定leaseTime参数。在指定的时间间隔后,锁定的锁将自动释放。
RLock对象的行为符合Java Lock规范。这意味着只有锁所有者线程才能解锁它,否则将引发IllegalMonitorStateException。否则,请考虑使用RSemaphore对象。
代码示例:
RReadWriteLock rwlock = redisson.getReadWriteLock("myLock");
RLock lock = rwlock.readLock();
// or
RLock lock = rwlock.writeLock();
// traditional lock method
lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
lock.unlock();
}
}
异步接口用法的代码示例:
RReadWriteLock rwlock = redisson.getReadWriteLock("myLock");
RLock lock = rwlock.readLock();
// or
RLock lock = rwlock.writeLock();
RFuture lockFuture = lock.lockAsync();
// or acquire lock and automatically unlock it after 10 seconds
RFuture lockFuture = lock.lockAsync(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
RFuture lockFuture = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);
lockFuture.whenComplete((res, exception) -> {
// ...
lock.unlockAsync();
});
反应式接口用法的代码示例:
RedissonReactiveClient redisson = redissonClient.reactive();
RReadWriteLockReactive rwlock = redisson.getReadWriteLock("myLock");
RLockReactive lock = rwlock.readLock();
// or
RLockReactive lock = rwlock.writeLock();
Mono lockMono = lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Mono lockMono = lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Mono lockMono = lock.tryLock(100, 10, TimeUnit.SECONDS);
lockMono.doOnNext(res -> {
// ...
})
.doFinally(lock.unlock())
.subscribe();
RxJava3 接口使用的代码示例:
RedissonRxClient redisson = redissonClient.rxJava();
RReadWriteLockRx rwlock = redisson.getReadWriteLock("myLock");
RLockRx lock = rwlock.readLock();
// or
RLockRx lock = rwlock.writeLock();
Completable lockRes = lock.lock();
// or acquire lock and automatically unlock it after 10 seconds
Completable lockRes = lock.lock(10, TimeUnit.SECONDS);
// or wait for lock aquisition up to 100 seconds
// and automatically unlock it after 10 seconds
Single lockRes = lock.tryLock(100, 10, TimeUnit.SECONDS);
lockRes.doOnSuccess(res -> {
// ...
})
.doFinally(lock.unlock())
.subscribe();
五、信号量
基于Redis的分布式Java信号量对象,类似于信号量对象。
可以在使用前初始化,但这不是必需的,通过trySetPermissions(permissions)方法获得可用的许可量。
代码示例:
RSemaphore semaphore = redisson.getSemaphore("mySemaphore");
// acquire single permit
semaphore.acquire();
// or acquire 10 permits
semaphore.acquire(10);
// or try to acquire permit
boolean res = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 seconds
boolean res = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire 10 permit
boolean res = semaphore.tryAcquire(10);
// or try to acquire 10 permits or wait up to 15 seconds
boolean res = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
if (res) {
try {
...
} finally {
semaphore.release();
}
}
异步接口用法的代码示例:
RSemaphore semaphore = redisson.getSemaphore("mySemaphore");
// acquire single permit
RFuture acquireFuture = semaphore.acquireAsync();
// or acquire 10 permits
RFuture acquireFuture = semaphore.acquireAsync(10);
// or try to acquire permit
RFuture acquireFuture = semaphore.tryAcquireAsync();
// or try to acquire permit or wait up to 15 seconds
RFuture acquireFuture = semaphore.tryAcquireAsync(15, TimeUnit.SECONDS);
// or try to acquire 10 permit
RFuture acquireFuture = semaphore.tryAcquireAsync(10);
// or try to acquire 10 permits or wait up to 15 seconds
RFuture acquireFuture = semaphore.tryAcquireAsync(10, 15, TimeUnit.SECONDS);
acquireFuture.whenComplete((res, exception) -> {
// ...
semaphore.releaseAsync();
});
反应式接口用法的代码示例:
RedissonReactiveClient redisson = redissonClient.reactive();
RSemaphoreReactive semaphore = redisson.getSemaphore("mySemaphore");
// acquire single permit
Mono acquireMono = semaphore.acquire();
// or acquire 10 permits
Mono acquireMono = semaphore.acquire(10);
// or try to acquire permit
Mono acquireMono = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 seconds
Mono acquireMono = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire 10 permit
Mono acquireMono = semaphore.tryAcquire(10);
// or try to acquire 10 permits or wait up to 15 seconds
Mono acquireMono = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
acquireMono.doOnNext(res -> {
// ...
})
.doFinally(semaphore.release())
.subscribe();
RxJava3 接口使用的代码示例:
RedissonRxClient redisson = redissonClient.rxJava();
RSemaphoreRx semaphore = redisson.getSemaphore("mySemaphore");
// acquire single permit
Completable acquireRx = semaphore.acquire();
// or acquire 10 permits
Completable acquireRx = semaphore.acquire(10);
// or try to acquire permit
Single acquireRx = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 seconds
Single acquireRx = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire 10 permit
Single acquireRx = semaphore.tryAcquire(10);
// or try to acquire 10 permits or wait up to 15 seconds
Single acquireRx = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
acquireRx.doOnSuccess(res -> {
// ...
})
.doFinally(semaphore.release())
.subscribe();
六、允许可过期信号量
基于 Redis 的分布式 Java 信号量对象,每个获取的许可证都支持租用时间参数。每个许可证都由自己的身份证标识,只能使用其身份证签发。
应在使用前通过trySetPermissions(permissions)方法使用可用许可量进行初始化。允许通过addPermissions(许可证)方法增加/减少可用许可证的数量。
代码示例:
RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
semaphore.trySetPermits(23);
// acquire permit
String id = semaphore.acquire();
// or acquire permit with lease time in 10 seconds
String id = semaphore.acquire(10, TimeUnit.SECONDS);
// or try to acquire permit
String id = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 seconds
String id = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire permit with least time 15 seconds or wait up to 10 seconds
String id = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
if (id != null) {
try {
...
} finally {
semaphore.release(id);
}
}
异步接口用法的代码示例:
RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
RFuture setFuture = semaphore.trySetPermitsAsync(23);
// acquire permit
RFuture acquireFuture = semaphore.acquireAsync();
// or acquire permit with lease time in 10 seconds
RFuture acquireFuture = semaphore.acquireAsync(10, TimeUnit.SECONDS);
// or try to acquire permit
RFuture acquireFuture = semaphore.tryAcquireAsync();
// or try to acquire permit or wait up to 15 seconds
RFuture acquireFuture = semaphore.tryAcquireAsync(15, TimeUnit.SECONDS);
// or try to acquire permit with least time 15 seconds or wait up to 10 seconds
RFuture acquireFuture = semaphore.tryAcquireAsync(10, 15, TimeUnit.SECONDS);
acquireFuture.whenComplete((id, exception) -> {
// ...
semaphore.releaseAsync(id);
});
反应式接口用法的代码示例:
RedissonReactiveClient redisson = redissonClient.reactive();
RPermitExpirableSemaphoreReactive semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
Mono setMono = semaphore.trySetPermits(23);
// acquire permit
Mono acquireMono = semaphore.acquire();
// or acquire permit with lease time in 10 seconds
Mono acquireMono = semaphore.acquire(10, TimeUnit.SECONDS);
// or try to acquire permit
Mono acquireMono = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 seconds
Mono acquireMono = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire permit with least time 15 seconds or wait up to 10 seconds
Mono acquireMono = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
acquireMono.flatMap(id -> {
// ...
return semaphore.release(id);
}).subscribe();
RxJava3 接口使用的代码示例:
RedissonRxClient redisson = redissonClient.rxJava();
RPermitExpirableSemaphoreRx semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
Single setRx = semaphore.trySetPermits(23);
// acquire permit
Single acquireRx = semaphore.acquire();
// or acquire permit with lease time in 10 seconds
Single acquireRx = semaphore.acquire(10, TimeUnit.SECONDS);
// or try to acquire permit
Maybe acquireRx = semaphore.tryAcquire();
// or try to acquire permit or wait up to 15 seconds
Maybe acquireRx = semaphore.tryAcquire(15, TimeUnit.SECONDS);
// or try to acquire permit with least time 15 seconds or wait up to 10 seconds
Maybe acquireRx = semaphore.tryAcquire(10, 15, TimeUnit.SECONDS);
acquireRx.flatMap(id -> {
// ...
return semaphore.release(id);
}).subscribe();
文章下方有交流学习区!一起学习进步!也可以前往官网,加入官方微信交流群
创作不易,如果觉得文章不错,可以点赞收藏评论
你的支持和鼓励是我创作的动力❗❗❗
官网:Doker 多克;官方旗舰店:首页-Doker 多克-淘宝网 全品优惠
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net