java基础-资源池模式

资源池模式有通用的几个指标,这也是资源池的通用设计模式,我们以apche-pool连接池为例,观察以下几个指标
setMaxActive
Sets the cap on the number of objects that can be allocated by the pool (checked out to clients, or idle awaiting checkout) at a given time. Use a negative value for no limit.
翻译:设置能被池(能检出给客户使用或者空闲等待检出)创建的对象的最大个数。使用负值就是不限制


setMaxWait
Sets the maximum amount of time (in milliseconds) the borrowObject method should block before throwing an exception when the pool is exhausted and the "when exhausted" action is WHEN_EXHAUSTED_BLOCK. When less than or equal to 0, the borrowObject method may block indefinitely.
翻译:当池消耗殆尽且消耗的动作是WHEN_EXHAUSTED_BLOCK,borrowobject函数在抛出异常前被阻止,可以设置的最大等待时间。如果小于0,borrowobject函数可能会无限期阻止。

setMaxIdle
Sets the cap on the number of "idle" instances in the pool. If maxIdle is set too low on heavily loaded systems it is possible you will see objects being destroyed and almost immediately new objects being created. This is a result of the active threads momentarily returning objects faster than they are requesting them them, causing the number of idle objects to rise above maxIdle. The best value for maxIdle for heavily loaded system will vary but the default is a good starting point.

翻译:设置池中的空闲实例阀值。如果在负载严重的系统上最大空闲设置的太小,你可能会看到对象刚被销毁立即就被创建。这是由于活跃线程瞬间产生对象的速度远大于他们需要对象,会造成大量空闲对象远超最大空闲值。对于负载严重的系统最佳值采用默认的开始值直到它发生变化。


setMinIdle
Sets the minimum number of objects allowed in the pool before the evictor thread (if active) spawns new objects. Note that no objects are created when numActive + numIdle >= maxActive. This setting has no effect if the idle object evictor is disabled (i.e. if timeBetweenEvictionRunsMillis <= 0).

翻译:在线程产生新对象被释放之前设置池中允许的最小对象,注意当numactive + numidle > = maxactive,将不会建立任何对象。如果空闲对象释放被禁止设置是没有效果的。比如timeBetweenEvictionRunsMillis 《 =0


WHEN_EXHAUSTED_FAIL 到达pool的阀值,失败的处理
WHEN_EXHAUSTED_BLOCK 到达pool的阀值,阻止再次借用对象的处理
WHEN_EXHAUSTED_GROW 到达pool的阀值,生成新的对象提供给外部调用者
DEFAULT_LIFO 默认是对象后进先出,也可以改成是先进先出


池的最外层有一个门闩,存储一个资源到池中,通知门闩,如果池中没有资源,则门闩会一直等待,直到有资源存储或者将资源返回给池将通知所有的门闩。这就是池模式的整个经典结构。


我们了解了上述指标,就会发现c3p0等资源池的实现其实都大同小异。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值