Class DefaultPooledObject<T>
java.lang.Object
cloud.opencode.base.pool.factory.DefaultPooledObject<T>
- Type Parameters:
T- the type of object being pooled - 池化对象类型
- All Implemented Interfaces:
PooledObject<T>
DefaultPooledObject - Default Pooled Object Implementation
DefaultPooledObject - 默认池化对象实现
Default implementation of PooledObject with thread-safe state management and timing tracking.
PooledObject的默认实现,具有线程安全的状态管理和时间追踪。
Features | 主要功能:
- Thread-safe state management with AtomicReference - 使用AtomicReference的线程安全状态管理
- Lock-free borrow counting with LongAdder - 使用LongAdder的无锁借用计数
- Zero-allocation nanoTime timestamps on hot path - 热路径上零分配的nanoTime时间戳
- CAS-based state transitions - 基于CAS的状态转换
Usage Examples | 使用示例:
DefaultPooledObject<Connection> pooled = new DefaultPooledObject<>(connection);
Connection conn = pooled.getObject();
pooled.markBorrowed();
// use connection
pooled.markReturned();
Performance | 性能特性:
- State transitions: O(1) CAS operation - 状态转换: O(1) CAS操作
- Counter updates: O(1) LongAdder - 计数更新: O(1) LongAdder
- markBorrowed/markReturned: zero allocation (nanoTime) - markBorrowed/markReturned: 零分配 (nanoTime)
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Lock-free: Yes - 无锁: 是
- Since:
- JDK 25, opencode-base-pool V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancompareAndSetState(PooledObjectState expect, PooledObjectState update) Compares and sets the state atomically.Gets the active duration (time since last borrow).longGets the borrow count.Gets the creation instant.Gets the idle duration (time since last return).Gets the last borrow instant.Gets the last return instant.Gets the last use instant.Gets the actual pooled object.getState()Gets the current state.voidMarks the object as borrowed (zero-allocation).voidmarkBorrowed(long nanoTime) Marks the object as borrowed with a pre-captured nanoTime (avoids extra System.nanoTime() call).voidMarks the object as returned (zero-allocation).voidsetState(PooledObjectState newState) Sets the state directly (use with caution).toString()
-
Constructor Details
-
DefaultPooledObject
Creates a new pooled object wrapper. 创建新的池化对象包装器。- Parameters:
object- the object to wrap - 要包装的对象
-
-
Method Details
-
getObject
Description copied from interface:PooledObjectGets the actual pooled object. 获取实际的池化对象。- Specified by:
getObjectin interfacePooledObject<T>- Returns:
- the object - 对象
-
getCreateInstant
Description copied from interface:PooledObjectGets the creation instant. 获取创建时间。- Specified by:
getCreateInstantin interfacePooledObject<T>- Returns:
- the creation instant - 创建时间
-
getLastBorrowInstant
Description copied from interface:PooledObjectGets the last borrow instant. 获取最后借用时间。- Specified by:
getLastBorrowInstantin interfacePooledObject<T>- Returns:
- the last borrow instant - 最后借用时间
-
getLastReturnInstant
Description copied from interface:PooledObjectGets the last return instant. 获取最后归还时间。- Specified by:
getLastReturnInstantin interfacePooledObject<T>- Returns:
- the last return instant - 最后归还时间
-
getLastUseInstant
Description copied from interface:PooledObjectGets the last use instant. 获取最后使用时间。- Specified by:
getLastUseInstantin interfacePooledObject<T>- Returns:
- the last use instant - 最后使用时间
-
getState
Description copied from interface:PooledObjectGets the current state. 获取当前状态。- Specified by:
getStatein interfacePooledObject<T>- Returns:
- the state - 状态
-
getBorrowCount
public long getBorrowCount()Description copied from interface:PooledObjectGets the borrow count. 获取借用次数。- Specified by:
getBorrowCountin interfacePooledObject<T>- Returns:
- the borrow count - 借用次数
-
getActiveDuration
Description copied from interface:PooledObjectGets the active duration (time since last borrow). 获取活跃时长(自上次借用以来的时间)。- Specified by:
getActiveDurationin interfacePooledObject<T>- Returns:
- the active duration - 活跃时长
-
getIdleDuration
Description copied from interface:PooledObjectGets the idle duration (time since last return). 获取空闲时长(自上次归还以来的时间)。- Specified by:
getIdleDurationin interfacePooledObject<T>- Returns:
- the idle duration - 空闲时长
-
compareAndSetState
Description copied from interface:PooledObjectCompares and sets the state atomically. 原子地比较并设置状态。- Specified by:
compareAndSetStatein interfacePooledObject<T>- Parameters:
expect- the expected state - 期望状态update- the new state - 新状态- Returns:
- true if successful - 如果成功返回true
-
markBorrowed
public void markBorrowed()Marks the object as borrowed (zero-allocation). 标记对象已被借用(零分配)。 -
markBorrowed
public void markBorrowed(long nanoTime) Marks the object as borrowed with a pre-captured nanoTime (avoids extra System.nanoTime() call). 使用预捕获的 nanoTime 标记对象已被借用(避免额外的 System.nanoTime() 调用)。- Parameters:
nanoTime- the pre-captured nanoTime value - 预捕获的 nanoTime 值
-
markReturned
public void markReturned()Marks the object as returned (zero-allocation). 标记对象已归还(零分配)。 -
setState
Sets the state directly (use with caution). 直接设置状态(谨慎使用)。- Parameters:
newState- the new state - 新状态
-
toString
-