Interface ObjectPool<T>
- Type Parameters:
T- the type of object being pooled - 池化对象类型
- All Superinterfaces:
AutoCloseable
- All Known Implementing Classes:
GenericObjectPool, SoftReferencePool, ThreadLocalPool, VirtualThreadPool
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 | 使用示例:
// PoolLease pattern (recommended, try-with-resources)
try (PoolLease<Connection> lease = pool.borrowLease()) {
Connection conn = lease.get();
// use connection
} // auto-return
// Execute pattern (also recommended)
String result = pool.execute(conn -> {
return conn.executeQuery("SELECT...");
});
// Manual borrow/return
Connection conn = pool.borrowObject();
try {
// use connection
} finally {
pool.returnObject(conn);
}
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 TypeMethodDescriptionvoidAdds a new object to the pool.Borrows an object wrapped in aPoolLeasefor try-with-resources.borrowLease(Duration timeout) Borrows an object wrapped in aPoolLeasewith timeout for try-with-resources.Borrows an object from the pool.borrowObject(Duration timeout) Borrows an object from the pool with timeout.voidclear()Clears all idle objects from the pool.default voidExecutes a void action with a pooled object (auto-return).default <R> RExecutes an action with a pooled object (auto-return).Gets the pool metrics.intGets the number of active (borrowed) objects.intGets the number of idle objects.voidinvalidateObject(T obj) Invalidates an object (will not be returned to pool).default voidpreparePool(int count) Pre-creates objects to warm up the pool.voidreturnObject(T obj) Returns an object to the pool.Methods inherited from interface AutoCloseable
close
-
Method Details
-
borrowObject
Borrows an object from the pool. 从池中借用对象。Uses default timeout from pool configuration.
使用池配置的默认超时。
- Returns:
- the borrowed object - 借用的对象
- Throws:
OpenPoolException- if borrowing fails - 如果借用失败
-
borrowObject
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
Returns an object to the pool. 将对象归还到池中。- Parameters:
obj- the object to return - 要归还的对象
-
invalidateObject
Invalidates an object (will not be returned to pool). 使对象失效(不会返回池中)。- Parameters:
obj- the object to invalidate - 要失效的对象
-
addObject
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
-
execute
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
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 - 如果执行失败
-
borrowLease
Borrows an object wrapped in aPoolLeasefor try-with-resources. 借用对象并包装在PoolLease中,用于 try-with-resources。Example | 示例:
try (PoolLease<Connection> lease = pool.borrowLease()) { Connection conn = lease.get(); // use connection } // auto-return- Returns:
- a pool lease wrapping the borrowed object - 包装借用对象的池租约
- Throws:
OpenPoolException- if borrowing fails - 如果借用失败
-
borrowLease
Borrows an object wrapped in aPoolLeasewith timeout for try-with-resources. 带超时借用对象并包装在PoolLease中,用于 try-with-resources。- Parameters:
timeout- the maximum wait time - 最大等待时间- Returns:
- a pool lease wrapping the borrowed object - 包装借用对象的池租约
- Throws:
OpenPoolException- if borrowing fails or times out - 如果借用失败或超时
-
preparePool
Pre-creates objects to warm up the pool. 预创建对象以预热池。Creates the specified number of objects and adds them to the pool. Useful for avoiding cold-start latency.
创建指定数量的对象并添加到池中。适用于避免冷启动延迟。
- Parameters:
count- the number of objects to pre-create - 要预创建的对象数量- Throws:
OpenPoolException- if object creation fails - 如果对象创建失败
-