Class DefaultLockMetrics

java.lang.Object
cloud.opencode.base.lock.metrics.DefaultLockMetrics
All Implemented Interfaces:
LockMetrics

public class DefaultLockMetrics extends Object implements 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    Gets the total number of successful lock acquisitions 获取成功锁获取的总次数
    Gets the average time spent waiting for the lock 获取等待锁的平均时间
    long
    Gets the number of times threads had to wait for the lock 获取线程必须等待锁的次数
    int
    Gets the current number of threads holding the lock 获取当前持有锁的线程数
    Gets the maximum time any thread spent waiting for the lock 获取任何线程等待锁的最大时间
    long
    Gets the total number of lock releases 获取锁释放的总次数
    long
    Gets the number of lock acquisition timeouts 获取锁获取超时次数
    void
    Records a successful lock acquisition 记录成功的锁获取
    void
    Records a lock release 记录锁释放
    void
    Records a lock acquisition timeout 记录锁获取超时
    void
    Resets all metrics to their initial values 重置所有指标为初始值
    Gets an immutable snapshot of the current metrics 获取当前指标的不可变快照

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • DefaultLockMetrics

      public DefaultLockMetrics()
  • Method Details

    • recordAcquire

      public void recordAcquire(Duration waitTime)
      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: LockMetrics
      Gets the total number of successful lock acquisitions 获取成功锁获取的总次数
      Specified by:
      getAcquireCount in interface LockMetrics
      Returns:
      total acquire count | 总获取次数
    • getReleaseCount

      public long getReleaseCount()
      Description copied from interface: LockMetrics
      Gets the total number of lock releases 获取锁释放的总次数
      Specified by:
      getReleaseCount in interface LockMetrics
      Returns:
      total release count | 总释放次数
    • getTimeoutCount

      public long getTimeoutCount()
      Description copied from interface: LockMetrics
      Gets the number of lock acquisition timeouts 获取锁获取超时次数
      Specified by:
      getTimeoutCount in interface LockMetrics
      Returns:
      timeout count | 超时次数
    • getContentionCount

      public long getContentionCount()
      Description copied from interface: LockMetrics
      Gets the number of times threads had to wait for the lock 获取线程必须等待锁的次数
      Specified by:
      getContentionCount in interface LockMetrics
      Returns:
      contention count (times waiting for lock) | 竞争次数(等待锁的次数)
    • getAverageWaitTime

      public Duration getAverageWaitTime()
      Description copied from interface: LockMetrics
      Gets the average time spent waiting for the lock 获取等待锁的平均时间
      Specified by:
      getAverageWaitTime in interface LockMetrics
      Returns:
      average wait time | 平均等待时间
    • getMaxWaitTime

      public Duration getMaxWaitTime()
      Description copied from interface: LockMetrics
      Gets the maximum time any thread spent waiting for the lock 获取任何线程等待锁的最大时间
      Specified by:
      getMaxWaitTime in interface LockMetrics
      Returns:
      max wait time | 最大等待时间
    • getCurrentHoldCount

      public int getCurrentHoldCount()
      Description copied from interface: LockMetrics
      Gets the current number of threads holding the lock 获取当前持有锁的线程数
      Specified by:
      getCurrentHoldCount in interface LockMetrics
      Returns:
      current hold count (if available) | 当前持有次数(如果可用)
    • snapshot

      public LockStats snapshot()
      Description copied from interface: LockMetrics
      Gets an immutable snapshot of the current metrics 获取当前指标的不可变快照
      Specified by:
      snapshot in interface LockMetrics
      Returns:
      metrics snapshot | 指标快照
    • reset

      public void reset()
      Description copied from interface: LockMetrics
      Resets all metrics to their initial values 重置所有指标为初始值
      Specified by:
      reset in interface LockMetrics