Class PoolContext
java.lang.Object
cloud.opencode.base.pool.PoolContext
Pool Context - ScopedValue-based Pool Context
池上下文 - 基于 ScopedValue 的池上下文
Uses JDK 25 ScopedValue for efficient context propagation in Virtual Threads.
使用 JDK 25 ScopedValue 实现虚拟线程中高效的上下文传播。
Features | 主要功能:
- ScopedValue-based context - 基于 ScopedValue 的上下文
- Virtual Thread friendly - 虚拟线程友好
- Borrow tracking - 借用追踪
- Context attributes - 上下文属性
Usage Examples | 使用示例:
PoolContext context = PoolContext.create("connectionPool");
PoolContext.run(context, () -> {
// Access current context
PoolContext current = PoolContext.current().orElseThrow();
System.out.println("Pool: " + current.poolName());
// Do work with pooled objects
return result;
});
Security | 安全性:
- Thread-safe: Partially (ConcurrentHashMap attributes, volatile fields) - 线程安全: 部分(ConcurrentHashMap属性,volatile字段)
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-pool V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ScopedValue<PoolContext> Current pool context (ScopedValue) 当前池上下文 (ScopedValue) -
Method Summary
Modifier and TypeMethodDescriptionGets borrow time 获取借用时间static PoolContextcreate()Creates a new pool context with default name 使用默认名称创建新的池上下文static PoolContextCreates a new pool context 创建新的池上下文Gets creation time 获取创建时间static Optional<PoolContext> current()Gets the current pool context 获取当前池上下文static PoolContextGets the current context or creates a new one 获取当前上下文或创建新的<T> Optional<T> getAttribute(String key) Gets an attribute 获取属性booleanhasAttribute(String key) Checks if attribute exists 检查属性是否存在booleanChecks if an object is currently borrowed 检查是否有对象当前被借用booleanChecks if on virtual thread 检查是否在虚拟线程上poolName()Gets the pool name 获取池名称voidrecordBorrow(Object object) Records object borrow 记录对象借用voidRecords object return 记录对象归还Gets return time 获取归还时间static voidrun(PoolContext context, Runnable task) Runs a runnable within this context 在此上下文中运行可运行对象static <T, X extends Throwable>
Trun(PoolContext context, ScopedValue.CallableOp<T, X> task) Runs a callable within this context 在此上下文中运行可调用对象setAttribute(String key, Object value) Sets an attribute 设置属性thread()Gets the thread 获取线程toString()
-
Field Details
-
CURRENT
Current pool context (ScopedValue) 当前池上下文 (ScopedValue)
-
-
Method Details
-
create
Creates a new pool context 创建新的池上下文- Parameters:
poolName- the pool name | 池名称- Returns:
- new context | 新上下文
-
create
Creates a new pool context with default name 使用默认名称创建新的池上下文- Returns:
- new context | 新上下文
-
run
public static <T, X extends Throwable> T run(PoolContext context, ScopedValue.CallableOp<T, X> task) throws XRuns a callable within this context 在此上下文中运行可调用对象- Type Parameters:
T- return type | 返回类型X- exception type | 异常类型- Parameters:
context- the pool context | 池上下文task- the task to run | 要运行的任务- Returns:
- task result | 任务结果
- Throws:
X- if task throws | 如果任务抛出异常
-
run
Runs a runnable within this context 在此上下文中运行可运行对象- Parameters:
context- the pool context | 池上下文task- the task to run | 要运行的任务
-
current
Gets the current pool context 获取当前池上下文- Returns:
- current context or empty | 当前上下文或空
-
currentOrCreate
Gets the current context or creates a new one 获取当前上下文或创建新的- Returns:
- current or new context | 当前或新的上下文
-
recordBorrow
Records object borrow 记录对象借用- Parameters:
object- the borrowed object | 借用的对象
-
recordReturn
public void recordReturn()Records object return 记录对象归还 -
hasBorrowedObject
public boolean hasBorrowedObject()Checks if an object is currently borrowed 检查是否有对象当前被借用- Returns:
- true if borrowed | 如果已借用返回 true
-
setAttribute
Sets an attribute 设置属性- Parameters:
key- attribute key | 属性键value- attribute value | 属性值- Returns:
- this context | 此上下文
-
getAttribute
-
hasAttribute
Checks if attribute exists 检查属性是否存在- Parameters:
key- attribute key | 属性键- Returns:
- true if exists | 如果存在返回 true
-
poolName
-
createdAt
-
thread
-
borrowTime
-
returnTime
-
isVirtualThread
public boolean isVirtualThread()Checks if on virtual thread 检查是否在虚拟线程上- Returns:
- true if virtual thread | 如果是虚拟线程返回 true
-
toString
-