Class SoftReferencePool<T>
java.lang.Object
cloud.opencode.base.pool.impl.SoftReferencePool<T>
- Type Parameters:
T- the type of object being pooled - 池化对象类型
- All Implemented Interfaces:
ObjectPool<T>, AutoCloseable
SoftReferencePool - Soft Reference Object Pool
SoftReferencePool - 软引用对象池
Object pool using soft references for idle objects. Objects can be garbage collected when memory is low, providing automatic memory management.
使用软引用存储空闲对象的对象池。当内存不足时对象可被垃圾回收,提供自动内存管理。
Features | 主要功能:
- Soft reference for idle objects - 空闲对象使用软引用
- Automatic memory reclaim - 自动内存回收
- GC-friendly pooling - GC友好的池化
- No hard limit on idle objects - 空闲对象无硬限制
Usage Examples | 使用示例:
SoftReferencePool<ExpensiveObject> pool = new SoftReferencePool<>(factory, config);
ExpensiveObject obj = pool.borrowObject();
try {
// use object
} finally {
pool.returnObject(obj);
}
// Object may be GC'd if memory is low
Performance | 性能特性:
- Lower memory footprint under pressure - 内存压力下占用更低
- May recreate objects after GC - GC后可能需要重建对象
Security | 安全性:
- Thread-safe: Yes (ConcurrentLinkedQueue, AtomicInteger) - 线程安全: 是(ConcurrentLinkedQueue,AtomicInteger)
- Null-safe: No - 空值安全: 否
- Since:
- JDK 25, opencode-base-pool V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSoftReferencePool(PooledObjectFactory<T> factory) Creates a soft reference pool with default configuration.SoftReferencePool(PooledObjectFactory<T> factory, PoolConfig config) Creates a soft reference pool with custom configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a new object to the pool.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()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).voidreturnObject(T obj) Returns an object to the pool.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ObjectPool
borrowLease, borrowLease, execute, execute, preparePool
-
Constructor Details
-
SoftReferencePool
Creates a soft reference pool with default configuration. 使用默认配置创建软引用池。- Parameters:
factory- the object factory - 对象工厂
-
SoftReferencePool
Creates a soft reference pool with custom configuration. 使用自定义配置创建软引用池。- Parameters:
factory- the object factory - 对象工厂config- the pool configuration - 池配置
-
-
Method Details
-
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 - 借用的对象
- Throws:
OpenPoolException- if borrowing fails - 如果借用失败
-
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 - 借用的对象
- Throws:
OpenPoolException- if borrowing fails or times out - 如果借用失败或超时
-
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 - 要失效的对象
-
addObject
Description copied from interface:ObjectPoolAdds a new object to the pool. 向池中添加新对象。- Specified by:
addObjectin interfaceObjectPool<T>- Throws:
OpenPoolException- if adding fails - 如果添加失败
-
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 - 指标
-
close
public void close()- Specified by:
closein interfaceAutoCloseable
-