public class RedisLock extends Object implements Lock, Serializable
class X {
public void m() {
Lock lock = new RedisLock(redisTemplate, "lockKey", 100);
lock.lock(); // block until acquire lock or timeout
try {
// ... method body
} finally {
lock.unlock()
}
}
}
class Y {
public void m() {
Lock lock = new RedisLock(redisTemplate, "lockKey", 100);
if (!lock.tryLock()) return;
try {
// ... method body
} finally {
lock.unlock();
}
}
}
class Z {
public void m() {
Lock lock = new RedisLock(redisTemplate, "lockKey", 100);
// auto timeout release lock
if (!lock.tryLock(100, TimeUnit.MILLISECONDS)) return;
try {
// ... method body
} finally {
lock.unlock();
}
}
}
| 构造器和说明 |
|---|
RedisLock(org.springframework.data.redis.core.RedisTemplate<?,?> redisTemplate,
String lockKey) |
RedisLock(org.springframework.data.redis.core.RedisTemplate<?,?> redisTemplate,
String lockKey,
int timeoutMillis) |
RedisLock(org.springframework.data.redis.core.RedisTemplate<?,?> redisTemplate,
String lockKey,
int timeoutMillis,
int sleepMillis)
Constructor
|
| 限定符和类型 | 方法和说明 |
|---|---|
static boolean |
exceptionContainsNoScriptError(Throwable e) |
void |
funlock()
Force unlock
|
boolean |
isHeldByCurrentThread()
当前线程是否持有锁
{@code
class X {
Lock lock = new RedisLock(redisTemplate, "lockKey", 100);
// ...
|
boolean |
isLocked()
是否已锁(任何线程)
|
void |
lock()
等待锁直到获取(non interrupt)
|
void |
lockInterruptibly()
等待锁直到获取成功或抛出InterruptedException异常
|
Condition |
newCondition() |
boolean |
tryLock()
尝试获取锁,成功返回true,失败返回false
|
boolean |
tryLock(long timeout,
TimeUnit unit)
尝试获取锁,成功返回true,失败返回false
线程中断则抛出interrupted异常 |
void |
unlock()
释放锁
|
public RedisLock(org.springframework.data.redis.core.RedisTemplate<?,?> redisTemplate,
String lockKey)
public RedisLock(org.springframework.data.redis.core.RedisTemplate<?,?> redisTemplate,
String lockKey,
int timeoutMillis)
public RedisLock(org.springframework.data.redis.core.RedisTemplate<?,?> redisTemplate,
String lockKey,
int timeoutMillis,
int sleepMillis)
redisTemplate - spring redis templatelockKey - the lock keytimeoutMillis - lock timeout millis seconds(prevent deadlock)sleepMillis - wait sleep millis secondspublic void lockInterruptibly()
throws InterruptedException
lockInterruptibly 在接口中 LockInterruptedException - if call Thread#interruptpublic boolean tryLock()
public boolean tryLock(long timeout,
@Nonnull
TimeUnit unit)
throws InterruptedException
tryLock 在接口中 Locktimeout - the timeout valueunit - the timeout unittrue lock successfullyInterruptedException - if interruptedpublic Condition newCondition()
newCondition 在接口中 Lockpublic boolean isHeldByCurrentThread()
class X {
Lock lock = new RedisLock(redisTemplate, "lockKey", 100);
// ...
public void m() {
assert !lock.isHeldByCurrentThread();
lock.lock();
try {
// ... method body
} finally {
lock.unlock();
}
}
}
true the current thread held lockedpublic boolean isLocked()
true is lockedpublic void funlock()
public static boolean exceptionContainsNoScriptError(Throwable e)
Copyright © 2023. All rights reserved.