Interface LockMetrics
- All Known Implementing Classes:
DefaultLockMetrics
public interface LockMetrics
Lock Metrics Interface for Lock Usage Statistics
锁使用统计的锁指标接口
Provides metrics about lock usage including acquire/release counts, contention, and timing information.
提供有关锁使用的指标,包括获取/释放次数、竞争和时间信息。
Features | 主要功能:
- Acquire/release counting - 获取/释放计数
- Timeout tracking - 超时跟踪
- Contention detection - 竞争检测
- Wait time statistics - 等待时间统计
- Snapshot support - 快照支持
Usage Examples | 使用示例:
Lock<Long> lock = OpenLock.lock(LockConfig.builder()
.enableMetrics(true)
.build());
// Get metrics | 获取指标
LockMetrics metrics = ((LocalLock) lock).getMetrics().orElseThrow();
// Check contention | 检查竞争
if (metrics.getContentionRate() > 0.5) {
// High contention | 高竞争
}
// Get snapshot | 获取快照
LockStats stats = metrics.snapshot();
Security | 安全性:
- Thread-safe: Implementation dependent - 线程安全: 依赖实现
- Since:
- JDK 25, opencode-base-lock V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongGets the total number of successful lock acquisitions 获取成功锁获取的总次数Gets the average time spent waiting for the lock 获取等待锁的平均时间longGets the number of times threads had to wait for the lock 获取线程必须等待锁的次数intGets the current number of threads holding the lock 获取当前持有锁的线程数Gets the maximum time any thread spent waiting for the lock 获取任何线程等待锁的最大时间longGets the total number of lock releases 获取锁释放的总次数longGets the number of lock acquisition timeouts 获取锁获取超时次数voidreset()Resets all metrics to their initial values 重置所有指标为初始值snapshot()Gets an immutable snapshot of the current metrics 获取当前指标的不可变快照
-
Method Details
-
getAcquireCount
long getAcquireCount()Gets the total number of successful lock acquisitions 获取成功锁获取的总次数- Returns:
- total acquire count | 总获取次数
-
getReleaseCount
long getReleaseCount()Gets the total number of lock releases 获取锁释放的总次数- Returns:
- total release count | 总释放次数
-
getTimeoutCount
long getTimeoutCount()Gets the number of lock acquisition timeouts 获取锁获取超时次数- Returns:
- timeout count | 超时次数
-
getContentionCount
long getContentionCount()Gets the number of times threads had to wait for the lock 获取线程必须等待锁的次数- Returns:
- contention count (times waiting for lock) | 竞争次数(等待锁的次数)
-
getAverageWaitTime
Duration getAverageWaitTime()Gets the average time spent waiting for the lock 获取等待锁的平均时间- Returns:
- average wait time | 平均等待时间
-
getMaxWaitTime
Duration getMaxWaitTime()Gets the maximum time any thread spent waiting for the lock 获取任何线程等待锁的最大时间- Returns:
- max wait time | 最大等待时间
-
getCurrentHoldCount
int getCurrentHoldCount()Gets the current number of threads holding the lock 获取当前持有锁的线程数- Returns:
- current hold count (if available) | 当前持有次数(如果可用)
-
snapshot
LockStats snapshot()Gets an immutable snapshot of the current metrics 获取当前指标的不可变快照- Returns:
- metrics snapshot | 指标快照
-
reset
void reset()Resets all metrics to their initial values 重置所有指标为初始值
-