Class NamedLockFactory
java.lang.Object
cloud.opencode.base.lock.manager.NamedLockFactory
Named Lock Factory with Striped Lock Pattern Support
支持条纹锁模式的命名锁工厂
Creates fine-grained locks based on names, supporting striped lock pattern to reduce memory usage while maintaining good concurrency.
基于名称创建细粒度锁,支持条纹锁模式以减少内存使用同时保持良好的并发性。
Features | 主要功能:
- Striped lock pattern for memory efficiency - 条纹锁模式提高内存效率
- Consistent hashing for name-to-lock mapping - 一致性哈希的名称到锁映射
- Optional unlimited named locks mode - 可选的无限命名锁模式
- Configurable stripe count - 可配置的条纹数
Usage Examples | 使用示例:
// Create factory with 64 stripes | 创建64条纹的工厂
NamedLockFactory factory = new NamedLockFactory(64);
// Execute with named lock | 使用命名锁执行
factory.execute("order:12345", () -> processOrder("12345"));
// Different keys may use different locks (concurrent) | 不同键可能使用不同锁(并发)
factory.execute("order:67890", () -> processOrder("67890"));
// Get result | 获取结果
Order order = factory.executeWithResult("order:12345", () -> loadOrder("12345"));
// Disable striping for unlimited locks | 禁用条纹获取无限锁
NamedLockFactory unlimited = new NamedLockFactory(0, false, LockConfig.defaults());
Performance | 性能特性:
- Striped mode: Fixed memory, good concurrency - 条纹模式:固定内存,良好并发
- Non-striped mode: Unlimited locks, higher memory - 非条纹模式:无限锁,更高内存
- Recommended stripes = 2x concurrent threads - 建议条纹数=并发线程数的2倍
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 factory with default 64 stripes and striping enabled 使用默认64条纹和启用条纹模式创建工厂NamedLockFactory(int stripes) Creates a factory with specified number of stripes 使用指定条纹数创建工厂NamedLockFactory(int stripes, boolean useStriping, LockConfig config) Creates a factory with full configuration 使用完整配置创建工厂 -
Method Summary
Modifier and TypeMethodDescriptionvoidExecutes action with the lock for the given name 使用给定名称的锁执行操作<R> RexecuteWithResult(String name, Supplier<R> supplier) Executes supplier with the lock for the given name and returns result 使用给定名称的锁执行并返回结果Gets a lock for the given name 获取给定名称的锁intGets the number of named locks created 获取已创建的命名锁数量intGets the number of stripes configured 获取配置的条纹数booleanChecks if striping mode is enabled 检查是否启用条纹模式
-
Constructor Details
-
NamedLockFactory
public NamedLockFactory()Creates a factory with default 64 stripes and striping enabled 使用默认64条纹和启用条纹模式创建工厂 -
NamedLockFactory
public NamedLockFactory(int stripes) Creates a factory with specified number of stripes 使用指定条纹数创建工厂- Parameters:
stripes- the number of stripes | 条纹数
-
NamedLockFactory
Creates a factory with full configuration 使用完整配置创建工厂- Parameters:
stripes- the number of stripes (must be positive) | 条纹数(必须为正数)useStriping- whether to use striping (false = unlimited named locks) | 是否使用条纹(false=无限命名锁)config- the lock configuration | 锁配置- Throws:
IllegalArgumentException- if stripes is not positive | 如果条纹数非正数则抛出异常
-
-
Method Details
-
getLock
Gets a lock for the given name 获取给定名称的锁If striping is enabled, uses consistent hashing to map names to a fixed pool of locks. Otherwise, creates a new lock per unique name.
如果启用条纹,使用一致性哈希将名称映射到固定的锁池。否则,为每个唯一名称创建新锁。
- Parameters:
name- the lock name | 锁名称- Returns:
- the lock | 锁
-
execute
-
executeWithResult
Executes supplier with the lock for the given name and returns result 使用给定名称的锁执行并返回结果- Type Parameters:
R- the result type | 结果类型- Parameters:
name- the lock name | 锁名称supplier- the supplier to execute | 要执行的供应者- Returns:
- the result | 结果
-
getStripes
public int getStripes()Gets the number of stripes configured 获取配置的条纹数- Returns:
- the number of stripes, or 0 if striping is disabled | 条纹数,如果禁用条纹则为0
-
getNamedLockCount
public int getNamedLockCount()Gets the number of named locks created 获取已创建的命名锁数量- Returns:
- the number of named locks (only meaningful if striping disabled) | 命名锁数量(仅在禁用条纹时有意义)
-
isStripingEnabled
public boolean isStripingEnabled()Checks if striping mode is enabled 检查是否启用条纹模式- Returns:
- true if striping is enabled | true表示启用条纹
-