Class PoolPartition<T>

java.lang.Object
org.miaixz.bus.core.lang.pool.partition.PoolPartition<T>
Type Parameters:
T - 对象类型
All Implemented Interfaces:
Closeable, Serializable, AutoCloseable, ObjectPool<T>

public class PoolPartition<T> extends Object implements ObjectPool<T>
对象池分区 一个分区实际为一个小的对象池,持有一个阻塞队列。 初始化时创建PoolConfig.getMinSize()个对象作为初始池对象.

当借出对象时,从队列头部取出并验证,验证通过后使用,验证不通过直接调用free(Poolable) 销毁并重新获取, 当池中对象都被借出(空了),创建新的对象并入队列,直到队列满为止,当满时等待归还,超时则报错。 当归还对象时,验证对象,不可用销毁之,可用入队列。 一个分区队列的实际

Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • PoolPartition

      public PoolPartition(PoolConfig config, BlockingQueue<Poolable<T>> queue, ObjectFactory<T> objectFactory)
      构造
      Parameters:
      config - 池配置
      queue - 阻塞队列类型
      objectFactory - 对象工厂,用于管理对象创建、检查和销毁
  • Method Details

    • borrowObject

      public Poolable<T> borrowObject()
      Description copied from interface: ObjectPool
      借出对象,流程如下:
      1. 从池中取出对象
      2. 检查对象可用性
      3. 如果无可用对象,扩容池并创建新对象
      4. 继续取对象
      Specified by:
      borrowObject in interface ObjectPool<T>
      Returns:
      对象
    • returnObject

      public PoolPartition<T> returnObject(Poolable<T> poolable)
      归还对象
      Specified by:
      returnObject in interface ObjectPool<T>
      Parameters:
      poolable - 归还的对象
      Returns:
      this
    • increase

      public int increase(int increaseSize)
      扩容并填充对象池队列 如果传入的扩容大小大于可用大小(即扩容大小加现有大小大于最大大小,则实际扩容到最大)
      Parameters:
      increaseSize - 扩容大小
      Returns:
      实际扩容大小,0表示已经达到最大,未成功扩容
    • free

      public PoolPartition<T> free(Poolable<T> obj)
      销毁对象,注意此方法操作的对象必须在队列外
      Parameters:
      obj - 被销毁的对象
      Returns:
      this
    • getTotal

      public int getTotal()
      Description copied from interface: ObjectPool
      获取持有对象总数(包括空闲对象 + 正在使用对象数)
      Specified by:
      getTotal in interface ObjectPool<T>
      Returns:
      总数
    • getIdleCount

      public int getIdleCount()
      Description copied from interface: ObjectPool
      获取空闲对象数,即在池中的对象数
      Specified by:
      getIdleCount in interface ObjectPool<T>
      Returns:
      空闲对象数,-1表示此信息不可用
    • getActiveCount

      public int getActiveCount()
      Description copied from interface: ObjectPool
      获取已经借出的对象(正在使用的)对象数
      Specified by:
      getActiveCount in interface ObjectPool<T>
      Returns:
      正在使用的对象数,-1表示此对象不可用
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • createPoolable

      protected Poolable<T> createPoolable()
      Returns:
      PartitionPoolable