public interface GlobalLock
Global locks, which can be implemented based on JVM, database, distributed, etc.
Because of the different lock implementations, it is not recommended to keep Lock references on a large scale.
It should getLock every time , in the following pattern.
A typical usage idiom for this method would be:
// (1) pattern try-resource
try(xxx.lock("...")){
// biz code
}
// (2) pattern lock-finally
final Lock lock = xxx.getLock("...");
lock.lock()
try {
// biz code
} finally {
lock.unlock();
}
// (3) pattern tryLock-finally
final Lock lock = xxx.getLock("...");
if (lock.tryLock()) {
try {
// biz code
} finally {
lock.unlock();
}
} else {
// perform alternative actions
}
| Modifier and Type | Interface and Description |
|---|---|
static class |
GlobalLock.AutoLock |
| Modifier and Type | Method and Description |
|---|---|
@NotNull Lock |
getLock(@NotNull String name)
Create a lock instance by its name.
|
default @NotNull GlobalLock.AutoLock |
lock(@NotNull String name)
syntax sugar for lock
|
@NotNull @NotNull Lock getLock(@NotNull @NotNull String name)
name - name of lock@NotNull default @NotNull GlobalLock.AutoLock lock(@NotNull @NotNull String name)
final Lock lock = xxx.getLock("...");
lock.lock();
try {
// biz code
} finally {
lock.unlock();
}
name - name of lockCopyright © 2024. All rights reserved.