对象池
| Modifiers | Name | Description |
|---|---|---|
static class |
ObjectPool.ObjectFactory |
| Type | Name and description |
|---|---|
static org.apache.commons.pool2.impl.GenericObjectPool |
create(Integer max, Closure maker, Closure activater = null, Closure passivater = null, Closure destroyer = null, Closure validator = null)创建一个对象池 |
| Methods inherited from class | Name |
|---|---|
class Object |
Object#wait(long, int), Object#wait(long), Object#wait(), Object#equals(Object), Object#toString(), Object#hashCode(), Object#getClass(), Object#notify(), Object#notifyAll() |
创建一个对象池
示例:// 创建一个最多两个元素的池子
def pool = ObjectPool.create(2, { return System.currentTimeMillis() )
// 打印当前时间,未释放
log.info pool.borrowObject()
// 获得对象、并释放对象
def obj = pool.borrowObject()
try {
...
\}\ finally {
pool.returnObject(obj)
\}\
// 打印当前时间,未释放
log.info pool.borrowObject()
// 超出当前数量,线程挂起等待
log.info pool.borrowObject()
}
max - 池子的容量maker - 创建并返回对象的闭包activater - 调用 borrowObject 获得对象后要执行的闭包,比如做一些初始化,可接收对象最为参数passivater - 调用 returnObject 是否对象后要执行的闭包,比如释放一些资源,可接收释放的对象作为参数destroyer - 销毁对象时要执行的闭包,可接收要销毁的对象作为参数validator - 创建、获取、释放、等待等情况下要执行的校验闭包,需要调用 pool.setTestXxxx(true) 以启用