Class GenericObjectPool<T>

java.lang.Object
cloud.opencode.base.pool.impl.GenericObjectPool<T>
Type Parameters:
T - the type of object being pooled - 池化对象类型
All Implemented Interfaces:
ObjectPool<T>, AutoCloseable

public class GenericObjectPool<T> extends Object implements ObjectPool<T>
GenericObjectPool - Generic Object Pool Implementation GenericObjectPool - 通用对象池实现

High-performance general-purpose object pool with configurable size, eviction, validation, and Virtual Thread support.

高性能通用对象池,支持可配置的大小、驱逐、验证和虚拟线程。

Features | 主要功能:

  • Semaphore-based concurrency control - 基于信号量的并发控制
  • ConcurrentLinkedDeque for idle objects - ConcurrentLinkedDeque存储空闲对象
  • Scheduled eviction with Virtual Threads - 使用虚拟线程的计划驱逐
  • Lock-free metrics collection - 无锁指标收集
  • Configurable validation points - 可配置的验证点

Usage Examples | 使用示例:

PooledObjectFactory<Connection> factory = new BasePooledObjectFactory<>() {
    @Override
    protected Connection create() {
        return DriverManager.getConnection(url);
    }
};

GenericObjectPool<Connection> pool = new GenericObjectPool<>(factory,
    PoolConfig.builder()
        .maxTotal(20)
        .testOnBorrow(true)
        .build());

Connection conn = pool.borrowObject();
try {
    // use connection
} finally {
    pool.returnObject(conn);
}

Performance | 性能特性:

  • Borrow: O(1) when idle available - 借用: O(1) 当有空闲时
  • Return: O(1) - 归还: O(1)
  • Virtual Thread friendly (Semaphore) - 虚拟线程友好(信号量)

Security | 安全性:

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

    • GenericObjectPool

      public GenericObjectPool(PooledObjectFactory<T> factory)
      Creates a pool with default configuration. 使用默认配置创建池。
      Parameters:
      factory - the object factory - 对象工厂
    • GenericObjectPool

      public GenericObjectPool(PooledObjectFactory<T> factory, PoolConfig config)
      Creates a pool with custom configuration. 使用自定义配置创建池。
      Parameters:
      factory - the object factory - 对象工厂
      config - the pool configuration - 池配置
  • Method Details

    • borrowObject

      public T borrowObject() throws OpenPoolException
      Description copied from interface: ObjectPool
      Borrows an object from the pool. 从池中借用对象。

      Uses default timeout from pool configuration.

      使用池配置的默认超时。

      Specified by:
      borrowObject in interface ObjectPool<T>
      Returns:
      the borrowed object - 借用的对象
      Throws:
      OpenPoolException - if borrowing fails - 如果借用失败
    • borrowObject

      public T borrowObject(Duration timeout) throws OpenPoolException
      Description copied from interface: ObjectPool
      Borrows an object from the pool with timeout. 带超时从池中借用对象。
      Specified by:
      borrowObject in interface ObjectPool<T>
      Parameters:
      timeout - the maximum wait time - 最大等待时间
      Returns:
      the borrowed object - 借用的对象
      Throws:
      OpenPoolException - if borrowing fails or times out - 如果借用失败或超时
    • returnObject

      public void returnObject(T obj)
      Description copied from interface: ObjectPool
      Returns an object to the pool. 将对象归还到池中。
      Specified by:
      returnObject in interface ObjectPool<T>
      Parameters:
      obj - the object to return - 要归还的对象
    • invalidateObject

      public void invalidateObject(T obj)
      Description copied from interface: ObjectPool
      Invalidates an object (will not be returned to pool). 使对象失效(不会返回池中)。
      Specified by:
      invalidateObject in interface ObjectPool<T>
      Parameters:
      obj - the object to invalidate - 要失效的对象
    • addObject

      public void addObject() throws OpenPoolException
      Description copied from interface: ObjectPool
      Adds a new object to the pool. 向池中添加新对象。
      Specified by:
      addObject in interface ObjectPool<T>
      Throws:
      OpenPoolException - if adding fails - 如果添加失败
    • getNumIdle

      public int getNumIdle()
      Description copied from interface: ObjectPool
      Gets the number of idle objects. 获取空闲对象数。
      Specified by:
      getNumIdle in interface ObjectPool<T>
      Returns:
      the idle count - 空闲数
    • getNumActive

      public int getNumActive()
      Description copied from interface: ObjectPool
      Gets the number of active (borrowed) objects. 获取活跃(借出)对象数。
      Specified by:
      getNumActive in interface ObjectPool<T>
      Returns:
      the active count - 活跃数
    • clear

      public void clear()
      Description copied from interface: ObjectPool
      Clears all idle objects from the pool. 清除池中所有空闲对象。
      Specified by:
      clear in interface ObjectPool<T>
    • getMetrics

      public PoolMetrics getMetrics()
      Description copied from interface: ObjectPool
      Gets the pool metrics. 获取池指标。
      Specified by:
      getMetrics in interface ObjectPool<T>
      Returns:
      the metrics - 指标
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable