Interface ObjectPool<T>

Type Parameters:
T - the type of object being pooled - 池化对象类型
All Superinterfaces:
AutoCloseable
All Known Implementing Classes:
GenericObjectPool, SoftReferencePool, ThreadLocalPool, VirtualThreadPool

public interface ObjectPool<T> extends AutoCloseable
ObjectPool - Object Pool Interface ObjectPool - 对象池接口

Core interface for object pooling, providing borrow/return semantics with automatic resource management.

对象池化的核心接口,提供借用/归还语义和自动资源管理。

Features | 主要功能:

  • Object borrowing with timeout - 带超时的对象借用
  • Object returning - 对象归还
  • Object invalidation - 对象失效
  • Pool metrics access - 池指标访问
  • Execute pattern for auto-return - 自动归还的执行模式

Usage Examples | 使用示例:

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

// Execute pattern (recommended)
String result = pool.execute(conn -> {
    return conn.executeQuery("SELECT...");
});

Security | 安全性:

  • Thread-safe: Implementation dependent - 线程安全: 取决于实现
Since:
JDK 25, opencode-base-pool V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a new object to the pool.
    Borrows an object from the pool.
    Borrows an object from the pool with timeout.
    void
    Clears all idle objects from the pool.
    default void
    execute(Consumer<T> action)
    Executes a void action with a pooled object (auto-return).
    default <R> R
    execute(Function<T,R> action)
    Executes an action with a pooled object (auto-return).
    Gets the pool metrics.
    int
    Gets the number of active (borrowed) objects.
    int
    Gets the number of idle objects.
    void
    Invalidates an object (will not be returned to pool).
    void
    Returns an object to the pool.

    Methods inherited from interface AutoCloseable

    close
  • Method Details

    • borrowObject

      T borrowObject() throws OpenPoolException
      Borrows an object from the pool. 从池中借用对象。

      Uses default timeout from pool configuration.

      使用池配置的默认超时。

      Returns:
      the borrowed object - 借用的对象
      Throws:
      OpenPoolException - if borrowing fails - 如果借用失败
    • borrowObject

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

      void returnObject(T obj)
      Returns an object to the pool. 将对象归还到池中。
      Parameters:
      obj - the object to return - 要归还的对象
    • invalidateObject

      void invalidateObject(T obj)
      Invalidates an object (will not be returned to pool). 使对象失效(不会返回池中)。
      Parameters:
      obj - the object to invalidate - 要失效的对象
    • addObject

      void addObject() throws OpenPoolException
      Adds a new object to the pool. 向池中添加新对象。
      Throws:
      OpenPoolException - if adding fails - 如果添加失败
    • getNumIdle

      int getNumIdle()
      Gets the number of idle objects. 获取空闲对象数。
      Returns:
      the idle count - 空闲数
    • getNumActive

      int getNumActive()
      Gets the number of active (borrowed) objects. 获取活跃(借出)对象数。
      Returns:
      the active count - 活跃数
    • clear

      void clear()
      Clears all idle objects from the pool. 清除池中所有空闲对象。
    • getMetrics

      PoolMetrics getMetrics()
      Gets the pool metrics. 获取池指标。
      Returns:
      the metrics - 指标
    • execute

      default <R> R execute(Function<T,R> action) throws OpenPoolException
      Executes an action with a pooled object (auto-return). 使用池化对象执行操作(自动归还)。

      Example | 示例:

      String result = pool.execute(conn -> {
          return conn.executeQuery("SELECT...");
      });
      
      Type Parameters:
      R - the result type - 结果类型
      Parameters:
      action - the action to execute - 要执行的操作
      Returns:
      the action result - 操作结果
      Throws:
      OpenPoolException - if execution fails - 如果执行失败
    • execute

      default void execute(Consumer<T> action) throws OpenPoolException
      Executes a void action with a pooled object (auto-return). 使用池化对象执行无返回值操作(自动归还)。

      Example | 示例:

      pool.execute(conn -> {
          conn.executeUpdate("UPDATE...");
      });
      
      Parameters:
      action - the action to execute - 要执行的操作
      Throws:
      OpenPoolException - if execution fails - 如果执行失败