Class LocalReadWriteLock

java.lang.Object
cloud.opencode.base.lock.local.LocalReadWriteLock
All Implemented Interfaces:
ReadWriteLock<Long>

public class LocalReadWriteLock extends Object implements ReadWriteLock<Long>
Local Read-Write Lock Implementation Based on JDK ReentrantReadWriteLock 基于JDK ReentrantReadWriteLock的本地读写锁实现

A read-write lock that allows multiple concurrent readers while ensuring exclusive access for writers. Virtual Thread friendly.

允许多个并发读取者同时访问,同时确保写入者独占访问的读写锁。虚拟线程友好。

Features | 主要功能:

  • Multiple concurrent readers - 多个并发读取者
  • Exclusive writer access - 独占写入访问
  • Fair/unfair lock modes - 公平/非公平锁模式
  • Virtual Thread friendly - 虚拟线程友好
  • Convenient execute methods - 便捷的执行方法

Usage Examples | 使用示例:

// Create read-write lock | 创建读写锁
LocalReadWriteLock rwLock = new LocalReadWriteLock();

// Read operation (multiple readers allowed) | 读操作(允许多个读取者)
String data = rwLock.executeRead(() -> loadData());

// Write operation (exclusive access) | 写操作(独占访问)
rwLock.executeWrite(() -> saveData(newData));

// Manual lock management | 手动锁管理
try (var guard = rwLock.readLock().lock()) {
    // Read operations | 读操作
}

// Fair lock for preventing starvation | 防止饥饿的公平锁
LocalReadWriteLock fairLock = new LocalReadWriteLock(
    LockConfig.builder().fair(true).build());

Performance | 性能特性:

  • High read throughput with concurrent readers - 并发读取时高读取吞吐量
  • Write operations serialize access - 写操作序列化访问

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Virtual Thread friendly: Yes - 虚拟线程友好: 是
Since:
JDK 25, opencode-base-lock V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • LocalReadWriteLock

      public LocalReadWriteLock()
      Creates a read-write lock with default configuration 使用默认配置创建读写锁
    • LocalReadWriteLock

      public LocalReadWriteLock(LockConfig config)
      Creates a read-write lock with specified configuration 使用指定配置创建读写锁
      Parameters:
      config - the lock configuration | 锁配置
  • Method Details

    • readLock

      public Lock<Long> readLock()
      Description copied from interface: ReadWriteLock
      Gets the read lock for concurrent read access 获取用于并发读取访问的读锁
      Specified by:
      readLock in interface ReadWriteLock<Long>
      Returns:
      the read lock | 读锁
    • writeLock

      public Lock<Long> writeLock()
      Description copied from interface: ReadWriteLock
      Gets the write lock for exclusive write access 获取用于独占写入访问的写锁
      Specified by:
      writeLock in interface ReadWriteLock<Long>
      Returns:
      the write lock | 写锁
    • getReadLockCount

      public int getReadLockCount()
      Gets the total number of read locks held 获取持有的读锁总数
      Returns:
      the number of read holds | 读锁持有数
    • getReadHoldCount

      public int getReadHoldCount()
      Gets the number of read locks held by the current thread 获取当前线程持有的读锁数量
      Returns:
      the number of read holds by current thread | 当前线程读锁持有数
    • isWriteLocked

      public boolean isWriteLocked()
      Checks if the write lock is held by any thread 检查写锁是否被任何线程持有
      Returns:
      true if write lock is held | true表示写锁被持有
    • isWriteLockedByCurrentThread

      public boolean isWriteLockedByCurrentThread()
      Checks if the write lock is held by the current thread 检查当前线程是否持有写锁
      Returns:
      true if write lock is held by current thread | true表示当前线程持有写锁
    • getWriteHoldCount

      public int getWriteHoldCount()
      Gets the number of write locks held by the current thread 获取当前线程持有的写锁数量
      Returns:
      the number of write holds | 写锁持有数