Class StripedLock.InnerLock
java.lang.Object
io.github.xezzon.zeroweb.common.concurrent.StripedLock.InnerLock
- All Implemented Interfaces:
LockAdaptor
- Enclosing class:
StripedLock
LockAdaptor 接口的内部实现,用于处理具体的锁操作。
它维护一个 Striped 实例,用于为不同的 ID 提供不同的锁。- Author:
- xezzon
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
InnerLock
public InnerLock(int timeout) 初始化StripedLock.InnerLock实例,设置超时时间。- Parameters:
timeout- 尝试获取锁时的超时时间(秒)。
-
-
Method Details
-
tryLock
尝试获取与给定 ID 关联的锁。
如果在指定的
timeout时间内成功获取锁,则执行supplier提供的方法并返回其结果。 如果获取锁失败(超时),则返回一个空的Optional。 在操作完成后,无论成功与否,都会释放锁。- Specified by:
tryLockin interfaceLockAdaptor- Type Parameters:
R-Supplier返回值的类型。- Parameters:
id- 锁的唯一标识符。supplier- 如果成功获取锁,将执行的Supplier。- Returns:
- 如果成功获取锁并执行
Supplier,则返回包含其结果的Optional;否则返回空的Optional。 - Throws:
ZerowebRuntimeException- 如果线程在等待锁的过程中被中断。
-