不推荐使用的CommonPoolTargetSource有一组您可以使用setWhenExhaustedActionName()定义的行为。它包含的CommonsPool2TargetSource和CommonsPool2 BaseGenericObjectPool将“getBlockWhenExhausted”描述为
返回是否在池耗尽时调用borrowObject()方法时阻塞
我想得那么多!这有什么用呢?
如果将CommonsPool设置为“WHEN_EXHAUSTED_GROW”或者只是抛出异常,Pool是否会扩展?会发生什么?
答案 0 :(得分:0)
正如耗尽的ObjectPool.borrowObject()行为文档中所述,是实施依赖的。
未严格指定池耗尽时此方法的行为(尽管可能由实现指定)。
CommonsPool2提供的GenericObjectPool
实现将在getBlockWhenExhausted()
为真时阻止,否则立即抛出NoSuchElementException
。 (如GenericObjectPoolborrowObject(long borrowMaxWaitMillis)中所述)
如果池已耗尽(没有可用的空闲实例且无法创建新实例),则此方法将阻止(如果BaseGenericObjectPool.getBlockWhenExhausted()为true)或抛出NoSuchElementException(如果BaseGenericObjectPool.getBlockWhenExhausted()为false )。 BaseGenericObjectPool.getBlockWhenExhausted()为true时此方法将阻塞的时间长度由传入borrowMaxWaitMillis参数的值确定。
与“WHEN_EXHAUSTED_GROW”类似的行为不可用,并且最有可能通过将maxTotal设置为-1
来达到,因此池根本无法耗尽。