Class LocalLock
java.lang.Object
cloud.opencode.base.lock.local.LocalLock
- All Implemented Interfaces:
Lock<Long>, AutoCloseable
Local Lock Implementation Based on JDK ReentrantLock
基于JDK ReentrantLock的本地锁实现
A high-performance local lock implementation with Virtual Thread support, avoiding synchronized keyword to prevent thread pinning.
高性能本地锁实现,支持虚拟线程,避免使用synchronized关键字以防止线程固定。
Features | 主要功能:
- Virtual Thread friendly (no pinning) - 虚拟线程友好(无固定)
- Reentrant lock support - 支持可重入锁
- Fair/unfair lock modes - 公平/非公平锁模式
- Configurable timeout - 可配置超时
- Built-in metrics collection - 内置指标收集
- Try-with-resources support - 支持try-with-resources
Usage Examples | 使用示例:
// Create lock with default configuration | 使用默认配置创建锁
LocalLock lock = new LocalLock();
// Execute with automatic release | 自动释放的执行
lock.execute(() -> {
// Critical section | 临界区
});
// Try-with-resources pattern | try-with-resources模式
try (var guard = lock.lock()) {
// Critical section | 临界区
}
// With custom configuration | 使用自定义配置
LocalLock fairLock = new LocalLock(LockConfig.builder()
.fair(true)
.enableMetrics(true)
.build());
Performance | 性能特性:
- Low contention overhead - 低争用开销
- Metrics tracking with minimal impact - 最小影响的指标跟踪
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Virtual Thread friendly: Yes - 虚拟线程友好: 是
- Reentrant: Configurable - 可重入: 可配置
- Since:
- JDK 25, opencode-base-lock V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a local lock with default configuration 使用默认配置创建本地锁LocalLock(LockConfig config) Creates a local lock with specified configuration 使用指定配置创建本地锁 -
Method Summary
Modifier and TypeMethodDescriptionintGets the hold count for current thread 获取当前线程的持有计数Gets the lock metrics if metrics collection is enabled 如果启用了指标收集,则获取锁指标intGets the estimated number of threads waiting to acquire this lock 获取等待获取此锁的线程估计数量getToken()Gets the current lock token 获取当前锁令牌booleanChecks if any threads are waiting to acquire this lock 检查是否有线程正在等待获取此锁booleanisFair()Checks if this lock uses fair ordering policy 检查此锁是否使用公平排序策略booleanChecks if lock is held by current thread 检查当前线程是否持有锁lock()Acquires the lock, blocking until available 获取锁,阻塞直到可用Acquires the lock with timeout 带超时获取锁Acquires the lock interruptibly 可中断地获取锁booleantryLock()Tries to acquire the lock immediately without waiting 立即尝试获取锁,不等待booleanTries to acquire the lock with timeout 带超时尝试获取锁voidunlock()Releases the lock 释放锁Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Lock
close, execute, execute, executeWithResult, executeWithResult
-
Constructor Details
-
LocalLock
public LocalLock()Creates a local lock with default configuration 使用默认配置创建本地锁 -
LocalLock
Creates a local lock with specified configuration 使用指定配置创建本地锁- Parameters:
config- the lock configuration | 锁配置
-
-
Method Details
-
lock
Description copied from interface:LockAcquires the lock, blocking until available 获取锁,阻塞直到可用Examples | 示例:
try (var guard = lock.lock()) { // Use the lock | 使用锁 } // Automatically released | 自动释放 -
lock
Description copied from interface:LockAcquires the lock with timeout 带超时获取锁Examples | 示例:
try (var guard = lock.lock(Duration.ofSeconds(5))) { // Use the lock | 使用锁 } -
tryLock
public boolean tryLock()Description copied from interface:LockTries to acquire the lock immediately without waiting 立即尝试获取锁,不等待Examples | 示例:
if (lock.tryLock()) { try { // Use the lock | 使用锁 } finally { lock.unlock(); } } -
tryLock
-
lockInterruptibly
Description copied from interface:LockAcquires the lock interruptibly 可中断地获取锁- Specified by:
lockInterruptiblyin interfaceLock<Long>- Returns:
- lock guard for auto-release | 用于自动释放的锁守卫
- Throws:
InterruptedException- if interrupted while waiting | 等待时被中断则抛出
-
unlock
-
isHeldByCurrentThread
public boolean isHeldByCurrentThread()Description copied from interface:LockChecks if lock is held by current thread 检查当前线程是否持有锁- Specified by:
isHeldByCurrentThreadin interfaceLock<Long>- Returns:
- true if current thread holds the lock | 当前线程持有锁返回true
-
getToken
-
getHoldCount
public int getHoldCount()Gets the hold count for current thread 获取当前线程的持有计数- Returns:
- the number of holds on this lock | 此锁的持有次数
-
isFair
public boolean isFair()Checks if this lock uses fair ordering policy 检查此锁是否使用公平排序策略- Returns:
- true if fair | true表示公平锁
-
hasQueuedThreads
public boolean hasQueuedThreads()Checks if any threads are waiting to acquire this lock 检查是否有线程正在等待获取此锁- Returns:
- true if there are waiting threads | true表示有等待线程
-
getQueueLength
public int getQueueLength()Gets the estimated number of threads waiting to acquire this lock 获取等待获取此锁的线程估计数量- Returns:
- the number of waiting threads | 等待线程数
-
getMetrics
Gets the lock metrics if metrics collection is enabled 如果启用了指标收集,则获取锁指标- Returns:
- lock metrics, or empty if metrics disabled | 锁指标,如果禁用则为空
-