Class ObservableLock<T>
java.lang.Object
cloud.opencode.base.lock.event.ObservableLock<T>
- Type Parameters:
T- the type of lock token | 锁令牌类型
- All Implemented Interfaces:
Lock<T>, AutoCloseable
Observable Lock Decorator with Event Notification
带事件通知的可观察锁装饰器
A decorator that wraps any Lock implementation and fires
LockEvent notifications to registered LockListeners
on lock lifecycle transitions.
一个装饰器,包装任意 Lock 实现,并在锁生命周期转换时
向注册的 LockListener 发送 LockEvent 通知。
Features | 主要功能:
- Transparent decorator pattern - 透明装饰器模式
- Thread-safe listener management - 线程安全的监听器管理
- Listener exception isolation - 监听器异常隔离
- Wait-time tracking for acquisition events - 获取事件的等待时间跟踪
- Fluent API for listener registration - 流式API用于监听器注册
Usage Examples | 使用示例:
// Wrap an existing lock | 包装现有锁
Lock<Long> baseLock = new LocalLock();
ObservableLock<Long> lock = new ObservableLock<>(baseLock, "order-lock");
// Add listeners | 添加监听器
lock.addListener(event -> log.info("{}: {}", event.type(), event.lockName()));
lock.addListener(event -> metrics.record(event));
// Use like a normal lock | 像普通锁一样使用
try (var guard = lock.lock()) {
// Critical section | 临界区
}
// ACQUIRED and RELEASED events are fired automatically
// ACQUIRED和RELEASED事件自动触发
// With initial listeners | 带初始监听器
ObservableLock<Long> lock2 = new ObservableLock<>(baseLock, "stock-lock",
auditListener, metricsListener);
Security | 安全性:
- Thread-safe: Yes (CopyOnWriteArrayList for listeners) - 线程安全: 是(监听器使用CopyOnWriteArrayList)
- Exception isolation: Listener exceptions are caught and suppressed - 异常隔离: 监听器异常被捕获并抑制
- Virtual Thread friendly: Yes - 虚拟线程友好: 是
- Since:
- JDK 25, opencode-base-lock V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionObservableLock(Lock<T> delegate, String lockName) Creates an observable lock wrapping the given delegate 创建包装给定委托的可观察锁ObservableLock(Lock<T> delegate, String lockName, LockListener... listeners) Creates an observable lock with initial listeners 创建带初始监听器的可观察锁 -
Method Summary
Modifier and TypeMethodDescriptionaddListener(LockListener listener) Adds a listener to receive lock events 添加监听器以接收锁事件Gets the lock name used in events 获取事件中使用的锁名称getToken()Gets the current lock token 获取当前锁令牌booleanChecks if lock is held by current thread 检查当前线程是否持有锁lock()Acquires the lock, blocking until available 获取锁,阻塞直到可用Acquires the lock with timeout 带超时获取锁Acquires the lock interruptibly 可中断地获取锁removeListener(LockListener listener) Removes a listener from receiving lock events 移除监听器使其不再接收锁事件booleantryLock()Tries to acquire the lock immediately without waiting 立即尝试获取锁,不等待booleanTries to acquire the lock with timeout 带超时尝试获取锁voidunlock()Releases the lock 释放锁Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Lock
close, execute, execute, executeWithResult, executeWithResult
-
Constructor Details
-
ObservableLock
Creates an observable lock wrapping the given delegate 创建包装给定委托的可观察锁- Parameters:
delegate- the underlying lock to decorate | 要装饰的底层锁lockName- the name for this lock (used in events) | 此锁的名称(用于事件)- Throws:
NullPointerException- if delegate or lockName is null | 如果delegate或lockName为null则抛出
-
ObservableLock
Creates an observable lock with initial listeners 创建带初始监听器的可观察锁- Parameters:
delegate- the underlying lock to decorate | 要装饰的底层锁lockName- the name for this lock (used in events) | 此锁的名称(用于事件)listeners- the initial listeners to register | 要注册的初始监听器- Throws:
NullPointerException- if delegate or lockName is null | 如果delegate或lockName为null则抛出
-
-
Method Details
-
addListener
Adds a listener to receive lock events 添加监听器以接收锁事件- Parameters:
listener- the listener to add | 要添加的监听器- Returns:
- this observable lock for fluent chaining | 此可观察锁用于链式调用
- Throws:
NullPointerException- if listener is null | 如果listener为null则抛出
-
removeListener
Removes a listener from receiving lock events 移除监听器使其不再接收锁事件- Parameters:
listener- the listener to remove | 要移除的监听器- Returns:
- this observable lock for fluent chaining | 此可观察锁用于链式调用
-
getLockName
Gets the lock name used in events 获取事件中使用的锁名称- Returns:
- the lock name | 锁名称
-
lock
Description copied from interface:LockAcquires the lock, blocking until available 获取锁,阻塞直到可用Examples | 示例:
try (var guard = lock.lock()) { // Use the lock | 使用锁 } // Automatically released | 自动释放 -
lock
Description copied from interface:LockAcquires the lock with timeout 带超时获取锁Examples | 示例:
try (var guard = lock.lock(Duration.ofSeconds(5))) { // Use the lock | 使用锁 } -
tryLock
public boolean tryLock()Description copied from interface:LockTries to acquire the lock immediately without waiting 立即尝试获取锁,不等待Examples | 示例:
if (lock.tryLock()) { try { // Use the lock | 使用锁 } finally { lock.unlock(); } } -
tryLock
-
lockInterruptibly
Description copied from interface:LockAcquires the lock interruptibly 可中断地获取锁- Specified by:
lockInterruptiblyin interfaceLock<T>- Returns:
- lock guard for auto-release | 用于自动释放的锁守卫
- Throws:
InterruptedException- if interrupted while waiting | 等待时被中断则抛出
-
unlock
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()Description copied from interface:LockChecks if lock is held by current thread 检查当前线程是否持有锁- Specified by:
isHeldByCurrentThreadin interfaceLock<T>- Returns:
- true if current thread holds the lock | 当前线程持有锁返回true
-
getToken
-