T
- 对象类型public class PoolPartition<T> extends Object implements ObjectPool<T>
PoolConfig.getMinSize()
个对象作为初始池对象.
当借出对象时,从队列头部取出并验证,验证通过后使用,验证不通过直接调用free(Object)
销毁并重新获取,
当池中对象都被借出(空了),创建新的对象并入队列,直到队列满为止,当满时等待归还,超时则报错。
当归还对象时,验证对象,不可用销毁之,可用入队列。
一个分区队列的实际
Constructor and Description |
---|
PoolPartition(PoolConfig config,
BlockingQueue<Poolable<T>> queue,
ObjectFactory<T> objectFactory)
构造
|
Modifier and Type | Method and Description |
---|---|
T |
borrowObject()
借出对象,流程如下:
从池中取出对象
检查对象可用性
如果无可用对象,扩容池并创建新对象
继续取对象
|
void |
close() |
protected Poolable<T> |
createPoolable()
|
PoolPartition<T> |
free(T obj)
销毁对象,注意此方法操作的对象必须在队列外
|
int |
getActiveCount()
获取已经借出的对象(正在使用的)对象数
|
int |
getIdleCount()
获取空闲对象数,即在池中的对象数
|
int |
getTotal()
获取持有对象总数(包括空闲对象 + 正在使用对象数)
|
int |
increase(int increaseSize)
扩容并填充对象池队列
如果传入的扩容大小大于可用大小(即扩容大小加现有大小大于最大大小,则实际扩容到最大) |
PoolPartition<T> |
returnObject(T obj)
归还对象
|
public PoolPartition(PoolConfig config, BlockingQueue<Poolable<T>> queue, ObjectFactory<T> objectFactory)
config
- 池配置queue
- 阻塞队列类型objectFactory
- 对象工厂,用于管理对象创建、检查和销毁public T borrowObject()
ObjectPool
borrowObject
in interface ObjectPool<T>
public PoolPartition<T> returnObject(T obj)
returnObject
in interface ObjectPool<T>
obj
- 归还的对象public int increase(int increaseSize)
increaseSize
- 扩容大小public PoolPartition<T> free(T obj)
free
in interface ObjectPool<T>
obj
- 被销毁的对象public int getTotal()
ObjectPool
getTotal
in interface ObjectPool<T>
public int getIdleCount()
ObjectPool
getIdleCount
in interface ObjectPool<T>
public int getActiveCount()
ObjectPool
getActiveCount
in interface ObjectPool<T>
public void close() throws IOException
close
in interface Closeable
close
in interface AutoCloseable
IOException
protected Poolable<T> createPoolable()
PartitionPoolable
Copyright © 2025. All rights reserved.