Record Class LockGuard<T>
java.lang.Object
java.lang.Record
cloud.opencode.base.lock.LockGuard<T>
- Type Parameters:
T- the type of lock token | 锁令牌类型- Record Components:
lock- the lock being held | 被持有的锁token- the lock token | 锁令牌
- All Implemented Interfaces:
AutoCloseable
Lock Guard for Automatic Resource Release
锁守卫 - 用于自动资源释放
A record that holds a lock and its token, implementing AutoCloseable for use with try-with-resources pattern to ensure automatic lock release.
持有锁及其令牌的记录,实现AutoCloseable接口, 配合try-with-resources模式确保自动释放锁。
Features | 主要功能:
- Automatic lock release - 自动释放锁
- Token access for lock identification - 令牌访问用于锁标识
- Exception-safe release - 异常安全释放
Usage Examples | 使用示例:
// Automatic release with try-with-resources | 使用try-with-resources自动释放
try (var guard = lock.lock()) {
// Critical section | 临界区
Long token = guard.token(); // Access token if needed | 如需要可访问令牌
} // Lock automatically released here | 锁在此自动释放
// Even on exception | 即使发生异常
try (var guard = lock.lock()) {
throw new RuntimeException("Error");
} // Lock still released | 锁仍然会释放
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Exception-safe: Yes - 异常安全: 是
- Since:
- JDK 25, opencode-base-lock V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Releases the lock when the guard is closed 当守卫关闭时释放锁final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.lock()Returns the value of thelockrecord component.token()Returns the value of thetokenrecord component.final StringtoString()Returns a string representation of this record class.
-
Constructor Details
-
LockGuard
-
-
Method Details
-
close
public void close()Releases the lock when the guard is closed 当守卫关闭时释放锁Called automatically when using try-with-resources.
使用try-with-resources时自动调用。
- Specified by:
closein interfaceAutoCloseable
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared withObjects::equals(Object,Object). -
lock
-
token
-