GenericObjectPoolConfig

GenericObjectPoolConfig是在GenericObjectPool构造的时候使用的,用来设置pool的maxTotal、maxIdle、minIdle等属性。

maxTotal比较好理解,表示pool中对象最多能有多少。主要是在create函数中创建对象时进行判断

int localMaxTotal = getMaxTotal();
long newCreateCount = createCount.incrementAndGet();
if (localMaxTotal > -1 && newCreateCount > localMaxTotal ||
        newCreateCount > Integer.MAX_VALUE) {
    createCount.decrementAndGet();
    return null;
}

maxIdle属性是指pool中最多能保留多少个空闲对象。这个属性主要是在returnObject函数中使用的。在程序使用完相关的对象后,会调用returnObject函数将对象返回到pool中。但是如果当前maxIdleSave <= idleObjects.size(),即当前pool中空闲对象的数量大于等于maxIdle时候,直接调用destroy函数来销毁对象

int maxIdleSave = getMaxIdle();
if (isClosed() || maxIdleSave > -1 && maxIdleSave <= idleObjects.size()) {
    try {
        destroy(p);
    } catch (Exception e) {
        swallowException(e);
    }
}

minIdle属性是指pool中最少有多少个空闲对象。该属性主要是在evict函数中调用。封装成EvictionConfig对象后,调用EvictionPolicy中的evict方法来判断是否需要回收当前测试的对象。

public boolean evict(EvictionConfig config, PooledObject<T> underTest,
            int idleCount) {

if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() &&
            config.getMinIdle() < idleCount) ||
            config.getIdleEvictTime() < underTest.getIdleTimeMillis()) {
        return true;
    }
    return false;
}

可以看出如果当前测试对象的空闲时间大于config中设置的idleSoftEvictTime并且pool中空闲对象的数量大于minIdle,那么就会return true。然后就会回收销毁该对象。
其中EvictionConfig中的idleSoftEvictTime和idleEvictTime的默认值是在BaseObjectPoolConfig中定义的。

/*** The default value for the {@code minEvictableIdleTimeMillis}* configuration attribute.* @see GenericObjectPool#getMinEvictableIdleTimeMillis()* @see GenericKeyedObjectPool#getMinEvictableIdleTimeMillis()*/
public static final long DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS =1000L * 60L * 30L;/*** The default value for the {@code softMinEvictableIdleTimeMillis}* configuration attribute.* @see GenericObjectPool#getSoftMinEvictableIdleTimeMillis()* @see GenericKeyedObjectPool#getSoftMinEvictableIdleTimeMillis()*/
public static final long DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS = -1;

所以IdleSoftEvictTime和IdleEvictTime的默认值分别是-1和半小时

evict函数是有一个定时任务定时去调用的。所以pool中一般会维持minIdle个闲置对象。所以如果当前pool中闲置对象的数量小于minIdle,pool并不会创建新的对象。minIdle只是用来回收对象的时候进行判断。

class Evictor extends TimerTask {/*** Run pool maintenance.  Evict objects qualifying for eviction and then* ensure that the minimum number of idle instances are available.* Since the Timer that invokes Evictors is shared for all Pools but* pools may exist in different class loaders, the Evictor ensures that* any actions taken are under the class loader of the factory* associated with the pool.*/@Overridepublic void run() {ClassLoader savedClassLoader =Thread.currentThread().getContextClassLoader();try {...// Evict from the pooltry {evict();} catch(Exception e) {swallowException(e);} catch(OutOfMemoryError oome) {// Log problem but give evictor thread a chance to continue// in case error is recoverableoome.printStackTrace(System.err);}// Re-create idle instances....} finally {// Restore the previous CCLThread.currentThread().setContextClassLoader(savedClassLoader);}}
}

该定时器是在GenericObjectPool的构造函数中调用的,调用间隔是由timeBetweenEvictionRunsMillis参数控制的。timeBetweenEvictionRunsMillis的默认值是-1L。这个时候定时器任务是不启用的,所以不会去调用evict来回收对象。

/*** <p>Starts the evictor with the given delay. If there is an evictor* running when this method is called, it is stopped and replaced with a* new evictor with the specified delay.</p>** <p>This method needs to be final, since it is called from a constructor.* See POOL-195.</p>** @param delay time in milliseconds before start and between eviction runs*/
final void startEvictor(long delay) {synchronized (evictionLock) {if (null != evictor) {EvictionTimer.cancel(evictor);evictor = null;evictionIterator = null;}if (delay > 0) {evictor = new Evictor();EvictionTimer.schedule(evictor, delay, delay);}}
}

commons-pool2中GenericObjectPoolConfig的maxTotal、maxIdle、minIdle属性理解相关推荐

  1. initialSize,maxTotal,maxIdle,minIdle,maxWaitMillis

    初始化连接数:默认值 0 同一时刻可分配最大连接数:默认值 8 ,设置为负数时不做限制 最大空闲连接,默认值 8 ,超出连接将被释放 最小空闲连接数,默认值 0 请求连接最大等待时间(毫秒),默认值 ...

  2. commons.pool2 对象池的使用

    commons.pool2 对象池的使用 ? 1 2 3 4 5 <dependency>     <groupId>org.apache.commons</groupI ...

  3. java.lang.NoClassDefFoundError: org/apache/commons/pool2/PooledObjectFactory

    用GenericObjectPool来连接postgres数据库,在pom.xml中也加入了依赖 <!-- https://mvnrepository.com/artifact/org.apac ...

  4. common pool2 mysql_连接池Commons Pool2的使用

    客户端这边,如果每次都临时建立一个新的连接,那么连接的开销非常大. 业内常用的连接池组件是 Commons Pool2---版本 2.4.2 packageservice.pool; importor ...

  5. React router 的 Route 中 component 和 render 属性理解

    React router 的 Route 中 component 和 render 属性理解 Route 标签的三个互斥属性 render.component.children Route 就是用来匹 ...

  6. DHTML中style的display和visibility属性

    DHTML中style的display和visibility属性 display是隐藏该对象,使该对象不占用页面排版空间. document.all("tr1").style.di ...

  7. python解包裹_关于Python中包裹传参和解包裹的理解

    原标题:关于Python中包裹传参和解包裹的理解 1.包裹传参 首先思考一个问题:为什么要有包裹传参?原因包括但不仅限于以下两点:①不确定参数的个数.②希望函数定义的更加松散灵活 包裹传参分两种:包裹 ...

  8. numpy中amin()方法中维度axis=0 1 2 的理解

    https://www.jianshu.com/p/6f58d7f39147 numpy中amin()方法中维度axis=0 1 2 的理解 axis=0  从最外一层的维度来比较 (对半比较  虽然 ...

  9. 相关子查询中exists后select 加数字的理解

      查看文章     相关子查询中exists后select 加数字的理解 2010-07-23 17:16 前提:两个基础表 SQL> select * from courses; COURS ...

最新文章

  1. const reference const
  2. python下拉菜单_python-web自动化:下拉列表操作
  3. 位置传感器:电位器,电容位置传感器
  4. es的forcemerge——按照天分割
  5. C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法
  6. 房价波动5%很正常 房地产市场绝不会崩盘
  7. snprintf函数用法
  8. Linux_ACL_su
  9. python字典去重合并_Python字典及基本操作(超级详细)
  10. android读写相册权限,androidQ 关于存储权限相册图片
  11. Java模拟HTTP的Get和Post请求(增强)
  12. VM安装rhel或linux后,声音很响,如何关闭
  13. Windows Apache Django 配置
  14. 1-1 二进制/源码/zip安装和升级
  15. HCIP-RS H12-221-题库包含答案 1-50题(不定期更新剩余题目)
  16. UE4 键盘输入浅析(一)
  17. 安装heg时找不到java,记录安装HEG的坑
  18. 高德地图定位及导航开发流程
  19. 十、如何给标识符命名
  20. 查壳去壳和加壳的使用指南

热门文章

  1. Java设计模式:23种设计模式全面解析,墙都不扶就服你
  2. SpringBoot自动装箱原理
  3. 那个说澳本聪是骗子的人胜诉了 挪威法院裁定Hodlonaut赢得了官司
  4. No_16_0324 Java基础学习第二十三天
  5. 七夕到了——属于Python的浪漫,拿去吧~ 祝表白成功
  6. 简历中linux内核开发,Linux内核中DMI实现简介
  7. js 对象添加动态属性并赋值
  8. 【Wordle】Day11:沉迷realo
  9. [转]博格坎普职业生涯全回顾:绿茵场上的哈利波特
  10. opengl从画三角形到画一个立方体(三)