Interface LockListener

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface LockListener
Lock Event Listener Interface 锁事件监听器接口

A functional interface for receiving lock lifecycle events. Implementations are notified when locks are acquired, released, timed out, or encounter errors.

用于接收锁生命周期事件的函数式接口。当锁被获取、释放、 超时或遇到错误时,实现会收到通知。

Features | 主要功能:

  • Functional interface for lambda usage - 函数式接口支持lambda使用
  • Event-driven lock monitoring - 事件驱动的锁监控
  • Decoupled lock observation - 解耦的锁观察

Usage Examples | 使用示例:

// Lambda listener | Lambda监听器
LockListener logger = event ->
    System.out.println(event.type() + ": " + event.lockName());

// Method reference listener | 方法引用监听器
LockListener metrics = this::recordMetrics;

// Register with observable lock | 注册到可观察锁
ObservableLock<Long> lock = new ObservableLock<>(delegate, "myLock");
lock.addListener(logger);

Security | 安全性:

  • Thread-safe: Implementation-dependent - 线程安全: 取决于实现
  • Exception isolation: Exceptions from listeners are caught and suppressed by ObservableLock - 异常隔离: 监听器异常由 ObservableLock 捕获并抑制
Since:
JDK 25, opencode-base-lock V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Called when a lock event occurs 当锁事件发生时调用
  • Method Details

    • onEvent

      void onEvent(LockEvent event)
      Called when a lock event occurs 当锁事件发生时调用

      Implementations should be lightweight and non-blocking to avoid impacting lock performance. Exceptions thrown by this method are caught and suppressed by ObservableLock.

      实现应轻量且非阻塞,以避免影响锁性能。此方法抛出的异常 由 ObservableLock 捕获并抑制。

      Parameters:
      event - the lock event | 锁事件