Class VirtualThreadPool<T>
java.lang.Object
cloud.opencode.base.pool.impl.VirtualThreadPool<T>
- Type Parameters:
T- the type of objects in the pool | 池中对象的类型
- All Implemented Interfaces:
ObjectPool<T>, AutoCloseable
VirtualThreadPool - Virtual Thread Friendly Object Pool
VirtualThreadPool - 虚拟线程友好的对象池
An object pool optimized for Virtual Threads with ScopedValue context propagation.
为虚拟线程优化的对象池,支持 ScopedValue 上下文传播。
Features | 主要功能:
- Virtual Thread optimized - 虚拟线程优化
- ScopedValue context propagation - ScopedValue 上下文传播
- Async borrow operations - 异步借用操作
- Auto-closeable lifecycle management - 自动关闭生命周期管理
Usage Examples | 使用示例:
VirtualThreadPool<Connection> pool = VirtualThreadPool.create(factory, config);
// Sync borrow
Connection conn = pool.borrowObject();
try {
// use connection
} finally {
pool.returnObject(conn);
}
// Async borrow
CompletableFuture<Connection> future = pool.borrowAsync();
future.thenAccept(conn -> {
// use connection
pool.returnObject(conn);
});
Security | 安全性:
- Thread-safe: Yes (Semaphore, ConcurrentHashMap) - 线程安全: 是(Semaphore,ConcurrentHashMap)
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-pool V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionVirtualThreadPool(PooledObjectFactory<T> factory, PoolConfig config) Creates a new VirtualThreadPool 创建新的 VirtualThreadPool -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new object to the pool.intGets available count 获取可用数量Borrows an object asynchronously 异步借用对象borrowAsync(Duration timeout) Borrows an object asynchronously with timeout 带超时的异步借用对象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.voidclose()static <T> VirtualThreadPool<T> create(PooledObjectFactory<T> factory, PoolConfig config) Creates a VirtualThreadPool with factory and config 使用工厂和配置创建 VirtualThreadPoolvoidExecutes a void action with a pooled object (auto-return).<R> RExecutes an action with a pooled object (auto-return).<R> CompletableFuture<R> executeAsync(Function<T, R> action) Executes action asynchronously 异步执行操作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).booleanChecks if running on virtual thread 检查是否运行在虚拟线程上voidreturnObject(T obj) Returns an object to the pool.intsize()Gets total created count 获取创建的总数Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ObjectPool
borrowLease, borrowLease, preparePool
-
Constructor Details
-
VirtualThreadPool
Creates a new VirtualThreadPool 创建新的 VirtualThreadPool- Parameters:
factory- the object factory | 对象工厂config- the pool config | 池配置
-
-
Method Details
-
create
Creates a VirtualThreadPool with factory and config 使用工厂和配置创建 VirtualThreadPool- Type Parameters:
T- object type | 对象类型- Parameters:
factory- the object factory | 对象工厂config- the pool config | 池配置- Returns:
- new pool | 新池
-
borrowObject
Description copied from interface:ObjectPoolBorrows an object from the pool. 从池中借用对象。Uses default timeout from pool configuration.
使用池配置的默认超时。
- Specified by:
borrowObjectin interfaceObjectPool<T>- Returns:
- the borrowed object - 借用的对象
-
borrowObject
Description copied from interface:ObjectPoolBorrows an object from the pool with timeout. 带超时从池中借用对象。- Specified by:
borrowObjectin interfaceObjectPool<T>- Parameters:
timeout- the maximum wait time - 最大等待时间- Returns:
- the borrowed object - 借用的对象
-
borrowAsync
Borrows an object asynchronously 异步借用对象- Returns:
- future with borrowed object | 包含借用对象的 Future
-
borrowAsync
Borrows an object asynchronously with timeout 带超时的异步借用对象- Parameters:
timeout- the timeout | 超时时间- Returns:
- future with borrowed object | 包含借用对象的 Future
-
returnObject
Description copied from interface:ObjectPoolReturns an object to the pool. 将对象归还到池中。- Specified by:
returnObjectin interfaceObjectPool<T>- Parameters:
obj- the object to return - 要归还的对象
-
invalidateObject
Description copied from interface:ObjectPoolInvalidates an object (will not be returned to pool). 使对象失效(不会返回池中)。- Specified by:
invalidateObjectin interfaceObjectPool<T>- Parameters:
obj- the object to invalidate - 要失效的对象
-
execute
Description copied from interface:ObjectPoolExecutes an action with a pooled object (auto-return). 使用池化对象执行操作(自动归还)。Example | 示例:
String result = pool.execute(conn -> { return conn.executeQuery("SELECT..."); });- Specified by:
executein interfaceObjectPool<T>- Type Parameters:
R- the result type - 结果类型- Parameters:
action- the action to execute - 要执行的操作- Returns:
- the action result - 操作结果
-
execute
Description copied from interface:ObjectPoolExecutes a void action with a pooled object (auto-return). 使用池化对象执行无返回值操作(自动归还)。Example | 示例:
pool.execute(conn -> { conn.executeUpdate("UPDATE..."); });- Specified by:
executein interfaceObjectPool<T>- Parameters:
action- the action to execute - 要执行的操作
-
executeAsync
Executes action asynchronously 异步执行操作- Type Parameters:
R- return type | 返回类型- Parameters:
action- the action | 操作- Returns:
- future with result | 包含结果的 Future
-
addObject
public void addObject()Description copied from interface:ObjectPoolAdds a new object to the pool. 向池中添加新对象。- Specified by:
addObjectin interfaceObjectPool<T>
-
getNumIdle
public int getNumIdle()Description copied from interface:ObjectPoolGets the number of idle objects. 获取空闲对象数。- Specified by:
getNumIdlein interfaceObjectPool<T>- Returns:
- the idle count - 空闲数
-
getNumActive
public int getNumActive()Description copied from interface:ObjectPoolGets the number of active (borrowed) objects. 获取活跃(借出)对象数。- Specified by:
getNumActivein interfaceObjectPool<T>- Returns:
- the active count - 活跃数
-
clear
public void clear()Description copied from interface:ObjectPoolClears all idle objects from the pool. 清除池中所有空闲对象。- Specified by:
clearin interfaceObjectPool<T>
-
getMetrics
Description copied from interface:ObjectPoolGets the pool metrics. 获取池指标。- Specified by:
getMetricsin interfaceObjectPool<T>- Returns:
- the metrics - 指标
-
size
public int size()Gets total created count 获取创建的总数- Returns:
- total count | 总数
-
available
public int available()Gets available count 获取可用数量- Returns:
- available count | 可用数量
-
isVirtualThread
public boolean isVirtualThread()Checks if running on virtual thread 检查是否运行在虚拟线程上- Returns:
- true if virtual thread | 如果是虚拟线程返回 true
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-