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>

public class PartitionObjectPool<T> extends Object implements ObjectPool<T>
分区对象池实现
Since:
Java 17+
Author:
Kimi Liu
See Also:
  • Constructor Details

    • PartitionObjectPool

      public PartitionObjectPool(PartitionPoolConfig config, ObjectFactory<T> factory)
      构造
      Parameters:
      config - 配置
      factory - 对象工厂,用于创建、验证和销毁对象
  • Method Details

    • getTotal

      public int getTotal()
      获取持有对象总数
      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表示此对象不可用
    • borrowObject

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

      public PartitionObjectPool<T> returnObject(Poolable<T> obj)
      Description copied from interface: ObjectPool
      归还对象,流程如下:
      1. 检查对象可用性
      2. 不可用则销毁之
      3. 可用则入池
      Specified by:
      returnObject in interface ObjectPool<T>
      Parameters:
      obj - 对象
      Returns:
      this
    • close

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

      protected BlockingQueue<Poolable<T>> createBlockingQueue(PartitionPoolConfig poolConfig)
      创建阻塞队列,默认为ArrayBlockingQueue 如果需要自定义队列类型,子类重写此方法
      Parameters:
      poolConfig - 池配置
      Returns:
      队列
    • getPartitionIndex

      protected int getPartitionIndex(PartitionPoolConfig poolConfig)
      获取当前线程被分配的分区 默认根据线程ID(TID)取分区大小余数 如果需要自定义,子类重写此方法
      Parameters:
      poolConfig - 池配置
      Returns:
      分配的分区