Class LockGroup

java.lang.Object
cloud.opencode.base.lock.manager.LockGroup
All Implemented Interfaces:
AutoCloseable

public class LockGroup extends Object implements AutoCloseable
Lock Group for Atomic Multi-Lock Acquisition with Deadlock Prevention 带死锁预防的原子多锁获取锁组

Supports atomically acquiring multiple locks with deadlock prevention through consistent lock ordering based on identity hash code.

支持通过基于身份哈希码的一致锁顺序来原子获取多个锁,防止死锁。

Features | 主要功能:

  • Atomic multi-lock acquisition - 原子多锁获取
  • Deadlock prevention via consistent ordering - 通过一致顺序防止死锁
  • Automatic rollback on failure - 失败时自动回滚
  • Try-with-resources support - 支持try-with-resources
  • Configurable timeout - 可配置超时

Usage Examples | 使用示例:

// Create lock group | 创建锁组
LockGroup group = LockGroup.builder()
    .add(lockA)
    .add(lockB)
    .add(lockC)
    .timeout(Duration.ofSeconds(10))
    .build();

// Atomic acquisition with auto-release | 自动释放的原子获取
try (var guard = group.lockAll()) {
    // All locks acquired atomically | 原子获取所有锁
    transferFunds(accountA, accountB, accountC);
}
// All locks automatically released | 所有锁自动释放

// Try without exception | 无异常尝试
if (group.tryLockAll()) {
    try {
        // Critical section | 临界区
    } finally {
        group.releaseAll();
    }
}

Deadlock Prevention | 死锁预防:

Locks are sorted by identity hash code before acquisition, ensuring all threads acquire locks in the same order, preventing circular wait.

锁在获取前按身份哈希码排序,确保所有线程以相同顺序获取锁,防止循环等待。

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Deadlock-free: Yes (with proper usage) - 无死锁: 是(正确使用时)
Since:
JDK 25, opencode-base-lock V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Lock Group Builder for Fluent API 锁组构建器 - 流式API
    static final record 
    Lock Group Guard for Automatic Resource Release 锁组守卫 - 用于自动资源释放
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Gets the number of currently acquired locks 获取当前已获取的锁数量
    Creates a new lock group builder 创建新的锁组构建器
    void
     
    Acquires all locks in the group atomically 原子获取组内所有锁
    void
    Releases all acquired locks in reverse order 按相反顺序释放所有已获取的锁
    int
    Gets the total number of locks in the group 获取锁组中的锁总数
    boolean
    Tries to acquire all locks without waiting 无等待尝试获取所有锁
    boolean
    Tries to acquire all locks with specified timeout 使用指定超时尝试获取所有锁

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • builder

      public static LockGroup.Builder builder()
      Creates a new lock group builder 创建新的锁组构建器
      Returns:
      the builder | 构建器
    • lockAll

      public LockGroup.LockGroupGuard lockAll()
      Acquires all locks in the group atomically 原子获取组内所有锁
      Returns:
      lock guard for automatic release | 用于自动释放的锁守卫
      Throws:
      OpenLockTimeoutException - if unable to acquire all locks | 如果无法获取所有锁则抛出异常
      OpenLockAcquireException - if lock acquisition fails | 如果锁获取失败则抛出异常
    • tryLockAll

      public boolean tryLockAll()
      Tries to acquire all locks without waiting 无等待尝试获取所有锁
      Returns:
      true if all locks acquired | true表示成功获取所有锁
    • tryLockAll

      public boolean tryLockAll(Duration timeout)
      Tries to acquire all locks with specified timeout 使用指定超时尝试获取所有锁
      Parameters:
      timeout - the timeout duration | 超时时长
      Returns:
      true if all locks acquired within timeout | true表示在超时内成功获取所有锁
    • releaseAll

      public void releaseAll()
      Releases all acquired locks in reverse order 按相反顺序释放所有已获取的锁
    • size

      public int size()
      Gets the total number of locks in the group 获取锁组中的锁总数
      Returns:
      the number of locks | 锁数量
    • acquiredCount

      public int acquiredCount()
      Gets the number of currently acquired locks 获取当前已获取的锁数量
      Returns:
      the number of acquired locks | 已获取的锁数量
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable