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

public class VirtualThreadPool<T> extends Object implements ObjectPool<T>
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 Details

    • VirtualThreadPool

      public VirtualThreadPool(PooledObjectFactory<T> factory, PoolConfig config)
      Creates a new VirtualThreadPool 创建新的 VirtualThreadPool
      Parameters:
      factory - the object factory | 对象工厂
      config - the pool config | 池配置
  • Method Details

    • create

      public static <T> VirtualThreadPool<T> create(PooledObjectFactory<T> factory, PoolConfig config)
      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

      public T borrowObject()
      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 - 借用的对象
    • borrowObject

      public T borrowObject(Duration timeout)
      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 - 借用的对象
    • borrowAsync

      public CompletableFuture<T> borrowAsync()
      Borrows an object asynchronously 异步借用对象
      Returns:
      future with borrowed object | 包含借用对象的 Future
    • borrowAsync

      public CompletableFuture<T> borrowAsync(Duration timeout)
      Borrows an object asynchronously with timeout 带超时的异步借用对象
      Parameters:
      timeout - the timeout | 超时时间
      Returns:
      future with borrowed object | 包含借用对象的 Future
    • 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 - 要失效的对象
    • execute

      public <R> R execute(Function<T,R> action)
      Description copied from interface: ObjectPool
      Executes an action with a pooled object (auto-return). 使用池化对象执行操作(自动归还)。

      Example | 示例:

      String result = pool.execute(conn -> {
          return conn.executeQuery("SELECT...");
      });
      
      Specified by:
      execute in interface ObjectPool<T>
      Type Parameters:
      R - the result type - 结果类型
      Parameters:
      action - the action to execute - 要执行的操作
      Returns:
      the action result - 操作结果
    • execute

      public void execute(Consumer<T> action)
      Description copied from interface: ObjectPool
      Executes a void action with a pooled object (auto-return). 使用池化对象执行无返回值操作(自动归还)。

      Example | 示例:

      pool.execute(conn -> {
          conn.executeUpdate("UPDATE...");
      });
      
      Specified by:
      execute in interface ObjectPool<T>
      Parameters:
      action - the action to execute - 要执行的操作
    • executeAsync

      public <R> CompletableFuture<R> executeAsync(Function<T,R> action)
      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: ObjectPool
      Adds a new object to the pool. 向池中添加新对象。
      Specified by:
      addObject in interface ObjectPool<T>
    • 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 - 指标
    • 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:
      close in interface AutoCloseable