您的位置:首页 > 编程语言

对象池commons-pool框架的研究以及源代码分析(三)

2013-04-21 16:27 387 查看
看完了StackObjectPool 类之后,我们再来看另外的一个对象池实现,即SoftReferenceObjectPool类,在详细看这个代码之前,先了解一下SoftReference类:

SoftReference:即对象的软引用,一个对象软引用后,如果虚拟机内存足够,垃圾回收线程就不会回收软引用对象的内存;与其相对应的是强引用,一个对象设置为强引用后,垃圾回收线程宁可报内存不足的错误,也不会回收其内存空间。

根据类名,我们可以就可以知道该对象池主要是管理软引用对象的,这对我们程序性能有很大的提高,一般我们都会将其应用在缓存方面,下面一起看看这个对象池的代码:

public
class SoftReferenceObjectPool extends BaseObjectPool implements ObjectPool {

//先看看属性

private
List _pool = null;//使用List(ArrayList)来保存对象

private PoolableObjectFactory _factory = null;//对象状态维护工厂

private int _numActive = 0;//总共有多少个对象被借用

public SoftReferenceObjectPool() {

_pool = new ArrayList();

_factory = null;

}

//构造函数

public SoftReferenceObjectPool(PoolableObjectFactory factory, int initSize) throws Exception {

_pool = new ArrayList();

_factory = factory;

if(null != _factory) {

for(int i=0;i<initSize;i++) {

Object obj = _factory.makeObject();

_factory.passivateObject(obj);//执行一下放入池后的动作

_pool.add(new SoftReference(obj));//可以看出,对象都是先生成软引用,再放入LIST中的

}

}

}

public synchronized Object borrowObject() throws Exception {

assertOpen();

Object obj = null;

while(null == obj) {

if(_pool.isEmpty()) {//对象池为空

if(null == _factory) {

throw new NoSuchElementException();

} else {

obj = _factory.makeObject();//直接生成一个对象

}

} else {//对象池不为空,取出数组里面最后一个,并GET出对象

SoftReference ref = (SoftReference)(_pool.remove(_pool.size() - 1));

obj = ref.get();

}

if(null != _factory && null != obj) {//如果取出对象

_factory.activateObject(obj);//激活对象

}

if (null != _factory && null != obj && !_factory.validateObject(obj)) {

_factory.destroyObject(obj);//如果对象已经失效,就销毁对象

obj = null;

}

}

_numActive++;

return obj;

}

//返回对象

public synchronized void returnObject(Object obj) throws Exception {

assertOpen();

boolean success = true;

if(!(_factory.validateObject(obj))) {//如果对象失效

success = false;

} else {//对象有效

try {

_factory.passivateObject(obj);//调用对象放回池中的处理方法

} catch(Exception e) {

success = false;

}

}

boolean shouldDestroy = !success;

_numActive--;

if(success) {

_pool.add(new SoftReference(obj));//放回池中

}

notifyAll(); // _numActive has changed

if(shouldDestroy) {

try {

_factory.destroyObject(obj);

} catch(Exception e) {

// ignored

}

}

}

//销毁对象

public synchronized void invalidateObject(Object obj) throws Exception {

assertOpen();

_numActive--;

_factory.destroyObject(obj);

notifyAll(); // _numActive has changed

}

//添加对象

public synchronized void addObject() throws Exception {

assertOpen();

Object obj = _factory.makeObject();

_numActive++; // 添加一个对象,但没有被借用,放回池的时候,减掉一个,但实际上不是借用者返回,所以这里加一个,确保不变。

returnObject(obj);

}

//获取池中有多少数据量

public synchronized int getNumIdle() {

assertOpen();

return _pool.size();

}

//获取总共有多少对象被借用

public synchronized int getNumActive() {

assertOpen();

return _numActive;

}

//销毁池中对象,并把对象池清空

public synchronized void clear() {

assertOpen();

if(null != _factory) {

Iterator iter = _pool.iterator();

while(iter.hasNext()) {

try {

Object obj = ((SoftReference)iter.next()).get();

if(null != obj) {

_factory.destroyObject(obj);

}

} catch(Exception e) {

// ignore error, keep destroying the rest

}

}

}

_pool.clear();

}

//关闭对象池

public synchronized void close() throws Exception {

clear();

_pool = null;

_factory = null;

super.close();

}

//设置对象状态管理工厂

public synchronized void setFactory(PoolableObjectFactory factory) throws IllegalStateException {

assertOpen();

if(0 < getNumActive()) {

throw new IllegalStateException("Objects are already active");

} else {

clear();

_factory = factory;

}

}

}

这个类就分析到这里,后面我们在看下一个比较难的类。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: