Class PartitionObjectPool<T>
java.lang.Object
org.miaixz.bus.core.lang.pool.partition.PartitionObjectPool<T>
- Type Parameters:
T- 对象类型
- All Implemented Interfaces:
Closeable,Serializable,AutoCloseable,ObjectPool<T>
分区对象池实现
- Since:
- Java 17+
- Author:
- Kimi Liu
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionPartitionObjectPool(PartitionPoolConfig config, ObjectFactory<T> factory) 构造 -
Method Summary
Modifier and TypeMethodDescription借出对象,流程如下: 从池中取出对象 检查对象可用性 如果无可用对象,扩容池并创建新对象 继续取对象voidclose()protected BlockingQueue<Poolable<T>> createBlockingQueue(PartitionPoolConfig poolConfig) 创建阻塞队列,默认为ArrayBlockingQueue如果需要自定义队列类型,子类重写此方法int获取已经借出的对象(正在使用的)对象数int获取空闲对象数,即在池中的对象数protected intgetPartitionIndex(PartitionPoolConfig poolConfig) 获取当前线程被分配的分区 默认根据线程ID(TID)取分区大小余数 如果需要自定义,子类重写此方法intgetTotal()获取持有对象总数returnObject(Poolable<T> obj) 归还对象,流程如下: 检查对象可用性 不可用则销毁之 可用则入池
-
Constructor Details
-
PartitionObjectPool
构造- Parameters:
config- 配置factory- 对象工厂,用于创建、验证和销毁对象
-
-
Method Details
-
getTotal
public int getTotal()获取持有对象总数- Specified by:
getTotalin interfaceObjectPool<T>- Returns:
- 总数
-
getIdleCount
public int getIdleCount()Description copied from interface:ObjectPool获取空闲对象数,即在池中的对象数- Specified by:
getIdleCountin interfaceObjectPool<T>- Returns:
- 空闲对象数,-1表示此信息不可用
-
getActiveCount
public int getActiveCount()Description copied from interface:ObjectPool获取已经借出的对象(正在使用的)对象数- Specified by:
getActiveCountin interfaceObjectPool<T>- Returns:
- 正在使用的对象数,-1表示此对象不可用
-
borrowObject
Description copied from interface:ObjectPool借出对象,流程如下:- 从池中取出对象
- 检查对象可用性
- 如果无可用对象,扩容池并创建新对象
- 继续取对象
- Specified by:
borrowObjectin interfaceObjectPool<T>- Returns:
- 对象
-
returnObject
Description copied from interface:ObjectPool归还对象,流程如下:- 检查对象可用性
- 不可用则销毁之
- 可用则入池
- Specified by:
returnObjectin interfaceObjectPool<T>- Parameters:
obj- 对象- Returns:
- this
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
createBlockingQueue
创建阻塞队列,默认为ArrayBlockingQueue如果需要自定义队列类型,子类重写此方法- Parameters:
poolConfig- 池配置- Returns:
- 队列
-
getPartitionIndex
获取当前线程被分配的分区 默认根据线程ID(TID)取分区大小余数 如果需要自定义,子类重写此方法- Parameters:
poolConfig- 池配置- Returns:
- 分配的分区
-