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

public record LockGuard<T>(Lock<T> lock, T token) extends Record implements 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
    Constructor
    Description
    LockGuard(Lock<T> lock, T token)
    Creates an instance of a LockGuard record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Releases the lock when the guard is closed 当守卫关闭时释放锁
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    Returns the value of the lock record component.
    Returns the value of the token record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • LockGuard

      public LockGuard(Lock<T> lock, T token)
      Creates an instance of a LockGuard record class.
      Parameters:
      lock - the value for the lock record component
      token - the value for the token record component
  • 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:
      close in interface AutoCloseable
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • lock

      public Lock<T> lock()
      Returns the value of the lock record component.
      Returns:
      the value of the lock record component
    • token

      public T token()
      Returns the value of the token record component.
      Returns:
      the value of the token record component