Class DefaultPooledObject<T>

java.lang.Object
cloud.opencode.base.pool.factory.DefaultPooledObject<T>
Type Parameters:
T - the type of object being pooled - 池化对象类型
All Implemented Interfaces:
PooledObject<T>

public class DefaultPooledObject<T> extends Object implements PooledObject<T>
DefaultPooledObject - Default Pooled Object Implementation DefaultPooledObject - 默认池化对象实现

Default implementation of PooledObject with thread-safe state management and timing tracking.

PooledObject的默认实现,具有线程安全的状态管理和时间追踪。

Features | 主要功能:

  • Thread-safe state management with AtomicReference - 使用AtomicReference的线程安全状态管理
  • Lock-free borrow counting with LongAdder - 使用LongAdder的无锁借用计数
  • Zero-allocation nanoTime timestamps on hot path - 热路径上零分配的nanoTime时间戳
  • CAS-based state transitions - 基于CAS的状态转换

Usage Examples | 使用示例:

DefaultPooledObject<Connection> pooled = new DefaultPooledObject<>(connection);
Connection conn = pooled.getObject();
pooled.markBorrowed();
// use connection
pooled.markReturned();

Performance | 性能特性:

  • State transitions: O(1) CAS operation - 状态转换: O(1) CAS操作
  • Counter updates: O(1) LongAdder - 计数更新: O(1) LongAdder
  • markBorrowed/markReturned: zero allocation (nanoTime) - markBorrowed/markReturned: 零分配 (nanoTime)

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Lock-free: Yes - 无锁: 是
Since:
JDK 25, opencode-base-pool V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • DefaultPooledObject

      public DefaultPooledObject(T object)
      Creates a new pooled object wrapper. 创建新的池化对象包装器。
      Parameters:
      object - the object to wrap - 要包装的对象
  • Method Details

    • getObject

      public T getObject()
      Description copied from interface: PooledObject
      Gets the actual pooled object. 获取实际的池化对象。
      Specified by:
      getObject in interface PooledObject<T>
      Returns:
      the object - 对象
    • getCreateInstant

      public Instant getCreateInstant()
      Description copied from interface: PooledObject
      Gets the creation instant. 获取创建时间。
      Specified by:
      getCreateInstant in interface PooledObject<T>
      Returns:
      the creation instant - 创建时间
    • getLastBorrowInstant

      public Instant getLastBorrowInstant()
      Description copied from interface: PooledObject
      Gets the last borrow instant. 获取最后借用时间。
      Specified by:
      getLastBorrowInstant in interface PooledObject<T>
      Returns:
      the last borrow instant - 最后借用时间
    • getLastReturnInstant

      public Instant getLastReturnInstant()
      Description copied from interface: PooledObject
      Gets the last return instant. 获取最后归还时间。
      Specified by:
      getLastReturnInstant in interface PooledObject<T>
      Returns:
      the last return instant - 最后归还时间
    • getLastUseInstant

      public Instant getLastUseInstant()
      Description copied from interface: PooledObject
      Gets the last use instant. 获取最后使用时间。
      Specified by:
      getLastUseInstant in interface PooledObject<T>
      Returns:
      the last use instant - 最后使用时间
    • getState

      public PooledObjectState getState()
      Description copied from interface: PooledObject
      Gets the current state. 获取当前状态。
      Specified by:
      getState in interface PooledObject<T>
      Returns:
      the state - 状态
    • getBorrowCount

      public long getBorrowCount()
      Description copied from interface: PooledObject
      Gets the borrow count. 获取借用次数。
      Specified by:
      getBorrowCount in interface PooledObject<T>
      Returns:
      the borrow count - 借用次数
    • getActiveDuration

      public Duration getActiveDuration()
      Description copied from interface: PooledObject
      Gets the active duration (time since last borrow). 获取活跃时长(自上次借用以来的时间)。
      Specified by:
      getActiveDuration in interface PooledObject<T>
      Returns:
      the active duration - 活跃时长
    • getIdleDuration

      public Duration getIdleDuration()
      Description copied from interface: PooledObject
      Gets the idle duration (time since last return). 获取空闲时长(自上次归还以来的时间)。
      Specified by:
      getIdleDuration in interface PooledObject<T>
      Returns:
      the idle duration - 空闲时长
    • compareAndSetState

      public boolean compareAndSetState(PooledObjectState expect, PooledObjectState update)
      Description copied from interface: PooledObject
      Compares and sets the state atomically. 原子地比较并设置状态。
      Specified by:
      compareAndSetState in interface PooledObject<T>
      Parameters:
      expect - the expected state - 期望状态
      update - the new state - 新状态
      Returns:
      true if successful - 如果成功返回true
    • markBorrowed

      public void markBorrowed()
      Marks the object as borrowed (zero-allocation). 标记对象已被借用(零分配)。
    • markBorrowed

      public void markBorrowed(long nanoTime)
      Marks the object as borrowed with a pre-captured nanoTime (avoids extra System.nanoTime() call). 使用预捕获的 nanoTime 标记对象已被借用(避免额外的 System.nanoTime() 调用)。
      Parameters:
      nanoTime - the pre-captured nanoTime value - 预捕获的 nanoTime 值
    • markReturned

      public void markReturned()
      Marks the object as returned (zero-allocation). 标记对象已归还(零分配)。
    • setState

      public void setState(PooledObjectState newState)
      Sets the state directly (use with caution). 直接设置状态(谨慎使用)。
      Parameters:
      newState - the new state - 新状态
    • toString

      public String toString()
      Overrides:
      toString in class Object