Class LockManager
java.lang.Object
cloud.opencode.base.lock.manager.LockManager
- All Implemented Interfaces:
AutoCloseable
Lock Manager for Centralized Lock Creation and Management
集中式锁创建和管理的锁管理器
Provides centralized management for creating and retrieving locks by name, supporting both local locks and read-write locks.
提供按名称创建和检索锁的集中管理,支持本地锁和读写锁。
Features | 主要功能:
- Named lock management - 命名锁管理
- Lazy lock creation - 延迟锁创建
- Local and read-write lock support - 支持本地锁和读写锁
- Lock lifecycle management - 锁生命周期管理
- AutoCloseable for cleanup - AutoCloseable支持清理
Usage Examples | 使用示例:
// Create lock manager | 创建锁管理器
LockManager manager = new LockManager();
// Get or create a local lock | 获取或创建本地锁
Lock<Long> lock = manager.getLocalLock("user:123");
lock.execute(() -> updateUser("123"));
// Get or create a read-write lock | 获取或创建读写锁
ReadWriteLock<Long> rwLock = manager.getLocalReadWriteLock("config");
rwLock.executeRead(() -> loadConfig());
// Direct execute | 直接执行
manager.executeWithLocalLock("order:456", () -> processOrder("456"));
// Cleanup | 清理
manager.close();
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Virtual Thread friendly: Yes - 虚拟线程友好: 是
- Since:
- JDK 25, opencode-base-lock V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a lock manager with default configuration 使用默认配置创建锁管理器LockManager(LockConfig localConfig) Creates a lock manager with specified configuration 使用指定配置创建锁管理器LockManager(LockConfig localConfig, int maxLocks) Creates a lock manager with specified configuration and maximum lock count 使用指定配置和最大锁数创建锁管理器 -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()voidexecuteWithLocalLock(String name, Runnable action) Executes action with a named local lock 使用命名本地锁执行操作getLocalLock(String name) Gets or creates a local lock by name 按名称获取或创建本地锁getLocalReadWriteLock(String name) Gets or creates a local read-write lock by name 按名称获取或创建本地读写锁intGets the total count of managed locks 获取托管锁的总数Gets the names of all managed locks 获取所有托管锁的名称booleanChecks if a lock with the given name exists 检查给定名称的锁是否存在booleanremoveLock(String name) Removes a lock by name and closes it 按名称移除锁并关闭它
-
Constructor Details
-
LockManager
public LockManager()Creates a lock manager with default configuration 使用默认配置创建锁管理器 -
LockManager
Creates a lock manager with specified configuration 使用指定配置创建锁管理器- Parameters:
localConfig- the configuration for local locks | 本地锁配置
-
LockManager
Creates a lock manager with specified configuration and maximum lock count 使用指定配置和最大锁数创建锁管理器- Parameters:
localConfig- the configuration for local locks | 本地锁配置maxLocks- maximum number of managed locks | 最大托管锁数- Throws:
IllegalArgumentException- if maxLocks is not positive | 如果maxLocks非正数则抛出
-
-
Method Details
-
getLocalLock
-
getLocalReadWriteLock
Gets or creates a local read-write lock by name 按名称获取或创建本地读写锁- Parameters:
name- the lock name | 锁名称- Returns:
- the read-write lock | 读写锁
-
executeWithLocalLock
-
hasLock
Checks if a lock with the given name exists 检查给定名称的锁是否存在- Parameters:
name- the lock name | 锁名称- Returns:
- true if the lock exists | true表示锁存在
-
removeLock
Removes a lock by name and closes it 按名称移除锁并关闭它- Parameters:
name- the lock name | 锁名称- Returns:
- true if the lock was removed | true表示锁已移除
-
getManagedLockNames
-
getManagedLockCount
public int getManagedLockCount()Gets the total count of managed locks 获取托管锁的总数- Returns:
- the count | 数量
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-