Class DefaultLockMetrics
java.lang.Object
cloud.opencode.base.lock.metrics.DefaultLockMetrics
- All Implemented Interfaces:
LockMetrics
Default Lock Metrics Implementation with Lock-Free Statistics
使用无锁统计的默认锁指标实现
A thread-safe implementation using atomic operations and LongAdder for high-performance lock-free statistics collection.
使用原子操作和LongAdder的线程安全实现,用于高性能无锁统计收集。
Features | 主要功能:
- Lock-free statistics - 无锁统计
- High throughput with LongAdder - 使用LongAdder实现高吞吐量
- Accurate contention tracking - 精确竞争跟踪
- Max wait time tracking - 最大等待时间跟踪
- Snapshot support - 快照支持
Usage Examples | 使用示例:
DefaultLockMetrics metrics = new DefaultLockMetrics();
// Record lock acquisition | 记录锁获取
metrics.recordAcquire(Duration.ofMillis(5));
// Record lock release | 记录锁释放
metrics.recordRelease();
// Get statistics | 获取统计
LockStats stats = metrics.snapshot();
System.out.println("Contention rate: " + stats.getContentionRate());
Performance | 性能特性:
- Uses LongAdder for high-throughput counting - 使用LongAdder实现高吞吐量计数
- Minimal impact on lock performance - 对锁性能影响最小
Security | 安全性:
- Thread-safe: Yes (lock-free) - 线程安全: 是(无锁)
- Since:
- JDK 25, opencode-base-lock V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
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 获取锁获取超时次数voidrecordAcquire(Duration waitTime) Records a successful lock acquisition 记录成功的锁获取voidRecords a lock release 记录锁释放voidRecords a lock acquisition timeout 记录锁获取超时voidreset()Resets all metrics to their initial values 重置所有指标为初始值snapshot()Gets an immutable snapshot of the current metrics 获取当前指标的不可变快照
-
Constructor Details
-
DefaultLockMetrics
public DefaultLockMetrics()
-
-
Method Details
-
recordAcquire
Records a successful lock acquisition 记录成功的锁获取- Parameters:
waitTime- time spent waiting for the lock | 等待锁的时间
-
recordRelease
public void recordRelease()Records a lock release 记录锁释放 -
recordTimeout
public void recordTimeout()Records a lock acquisition timeout 记录锁获取超时 -
getAcquireCount
public long getAcquireCount()Description copied from interface:LockMetricsGets the total number of successful lock acquisitions 获取成功锁获取的总次数- Specified by:
getAcquireCountin interfaceLockMetrics- Returns:
- total acquire count | 总获取次数
-
getReleaseCount
public long getReleaseCount()Description copied from interface:LockMetricsGets the total number of lock releases 获取锁释放的总次数- Specified by:
getReleaseCountin interfaceLockMetrics- Returns:
- total release count | 总释放次数
-
getTimeoutCount
public long getTimeoutCount()Description copied from interface:LockMetricsGets the number of lock acquisition timeouts 获取锁获取超时次数- Specified by:
getTimeoutCountin interfaceLockMetrics- Returns:
- timeout count | 超时次数
-
getContentionCount
public long getContentionCount()Description copied from interface:LockMetricsGets the number of times threads had to wait for the lock 获取线程必须等待锁的次数- Specified by:
getContentionCountin interfaceLockMetrics- Returns:
- contention count (times waiting for lock) | 竞争次数(等待锁的次数)
-
getAverageWaitTime
Description copied from interface:LockMetricsGets the average time spent waiting for the lock 获取等待锁的平均时间- Specified by:
getAverageWaitTimein interfaceLockMetrics- Returns:
- average wait time | 平均等待时间
-
getMaxWaitTime
Description copied from interface:LockMetricsGets the maximum time any thread spent waiting for the lock 获取任何线程等待锁的最大时间- Specified by:
getMaxWaitTimein interfaceLockMetrics- Returns:
- max wait time | 最大等待时间
-
getCurrentHoldCount
public int getCurrentHoldCount()Description copied from interface:LockMetricsGets the current number of threads holding the lock 获取当前持有锁的线程数- Specified by:
getCurrentHoldCountin interfaceLockMetrics- Returns:
- current hold count (if available) | 当前持有次数(如果可用)
-
snapshot
Description copied from interface:LockMetricsGets an immutable snapshot of the current metrics 获取当前指标的不可变快照- Specified by:
snapshotin interfaceLockMetrics- Returns:
- metrics snapshot | 指标快照
-
reset
public void reset()Description copied from interface:LockMetricsResets all metrics to their initial values 重置所有指标为初始值- Specified by:
resetin interfaceLockMetrics
-