Class SegmentLock<K>
java.lang.Object
cloud.opencode.base.lock.local.SegmentLock<K>
- Type Parameters:
K- the key type | 键类型
Segment Lock Implementation for Fine-Grained Locking
细粒度锁定的分段锁实现
Divides the lock space into multiple segments to reduce lock contention. Keys are mapped to segments using consistent hashing, allowing operations on different keys to proceed concurrently.
将锁空间划分为多个分段以减少锁争用。使用一致性哈希将键映射到分段, 允许对不同键的操作并发进行。
Features | 主要功能:
- Reduces lock contention through partitioning - 通过分区减少锁争用
- Consistent hash-based key mapping - 基于一致性哈希的键映射
- Configurable number of segments - 可配置的分段数量
- Per-key locking semantics - 按键锁定语义
Usage Examples | 使用示例:
// Create segment lock with 32 segments | 创建32个分段的分段锁
SegmentLock<String> lock = new SegmentLock<>(32);
// Different keys may map to different segments (concurrent) | 不同键可能映射到不同分段(并发)
lock.execute("user:123", () -> updateUser("123"));
lock.execute("user:456", () -> updateUser("456"));
// Get result with lock | 带锁获取结果
User user = lock.executeWithResult("user:123", () -> loadUser("123"));
// Access underlying lock | 访问底层锁
Lock<Long> userLock = lock.getLock("user:123");
Performance | 性能特性:
- Higher concurrency with more segments - 更多分段提供更高并发性
- Memory trade-off: more segments = more memory - 内存权衡:更多分段=更多内存
- Recommended: segments = 2x expected 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 segment lock with default 16 segments 使用默认16个分段创建分段锁SegmentLock(int segments) Creates a segment lock with specified number of segments 使用指定分段数创建分段锁SegmentLock(int segments, LockConfig config) Creates a segment lock with specified segments and configuration 使用指定分段数和配置创建分段锁 -
Method Summary
Modifier and TypeMethodDescriptionvoidExecutes action with the lock for the specified key 使用指定键对应的锁执行操作<R> RexecuteWithResult(K key, Supplier<R> supplier) Executes supplier with the lock for the specified key and returns result 使用指定键对应的锁执行并返回结果Gets the lock for the specified key 获取指定键对应的锁intgetSegmentIndex(K key) Gets the segment index for the specified key 获取指定键对应的分段索引intGets the total number of segments 获取总分段数
-
Constructor Details
-
SegmentLock
public SegmentLock()Creates a segment lock with default 16 segments 使用默认16个分段创建分段锁 -
SegmentLock
public SegmentLock(int segments) Creates a segment lock with specified number of segments 使用指定分段数创建分段锁- Parameters:
segments- the number of segments | 分段数
-
SegmentLock
Creates a segment lock with specified segments and configuration 使用指定分段数和配置创建分段锁- Parameters:
segments- the number of segments (must be positive) | 分段数(必须为正数)config- the lock configuration | 锁配置- Throws:
IllegalArgumentException- if segments is not positive | 如果分段数非正数则抛出异常
-
-
Method Details
-
getLock
-
execute
-
executeWithResult
-
getSegments
public int getSegments()Gets the total number of segments 获取总分段数- Returns:
- the number of segments | 分段数
-
getSegmentIndex
Gets the segment index for the specified key 获取指定键对应的分段索引- Parameters:
key- the key | 键- Returns:
- the segment index (0 to segments-1) | 分段索引(0到segments-1)
-