common-pool简单解析

apache common-pool的简单解析,以来了解对象池中运行原理

1.pool的一个简单操作实例(后附)

2.对象池概要:

  主要为对象池ObjectPool,对象池工厂ObjectPoolFactory(产生对象池,可根据不然参数构造create(...)),池对象 poolobject(即需要复用的对象),池对象工厂PoolableObjectFactory(池对象的定义,如类别,key)

 

3.common-pool中的各种类的区分解析

对象池:

按照数据结构划分:

GenericObjectPool,GenericKeyedObjectPool,内部数据结构为LinkedList,访问速度块,但是随即访问欠缺,且使用同步控制保证线程安全;

StatckObjectPool,keyedStatckobjectPool内部容器为statck,先入后出,且使用同步控制保证线程安全;

SoftreferenceObjectpool,内容容器为Arraylist,初始化全部对象,软引用依旧不能被jvm回收,缓存效果好,且使用同步控制保证线程安全;

按照KEY划分:

非key:GenericObjectPool,StatckObjectPool,SoftreferenceObjectpool,主要为产生同一类别的复用对象

key:GenericKeyedObjectPool,keyedStatckobjectPool,可以产生多个类别的复用对象

实现原理:

为 在池对象中使用不同与poolableobjectfactory的keypoolableobjectfactory,表现在方法中 makeobject(object o)带参数,然后在于如genericobjectpool中borrowobject()方法中调用makeobject(o)根据不同key来进行存 储于hashMap-->(key,linkedlist)

池对象工厂:

PoolableObjectFactory->basePoolableObjectFactory

keyedPoolableObjectFactory->baseKeyedPoolableObjectFactory

对象池:

objectpool->baseobjectpool->3种具体实现

keyedobjectpool->keyedbaseobjectpool->2种具体实现

操作为borrowobject,returnobject。clear

各种容器实现方式操作也不一样

arraylist一次全部初始化,构建全部对象,使用坐标来进行获取与归还

linkedlist,addlast,removefirst

stack:pop,push

后两者存储对象在returnobject中归还存储,borrowobject中调用makeobject给外界使用,但不立刻存储在容器中,并且池对象并不是以原始对象存储,需要经过GenericKeyedObjectPool.ObjectTimestampPair装饰一下,以.value返回存储

 

 

对象池工厂:5中对象池.create(...)构造

4.common-pool下载地址 http://pan.baidu.com/s/1kT8zjcr

建议对象池不要使用过多,容易造成入不敷出的情况,可以使用缓存,虽说对象池为缓存实现的一个模块

package pools;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.springframework.util.DigestUtils;

public class TestObject {
    public static void main(String args[]) {
        
        PoolableObjectFactory fac=new TestFactory();
        
        ObjectPool pool=new GenericObjectPool(fac);
        
        try{
            User user=(User) pool.borrowObject();
            user.setUsername("yaoge");
            user.setUserpass("share");
            user.print_info();
            pool.returnObject(user);
            
            User user1=(User) pool.borrowObject();
            user1.print_info();
            pool.returnObject(user1);
            
            
        }
        catch(Exception e) {
            System.err.println(e.toString());
        }
        
        
        
    }
}

package pools;

import org.apache.commons.pool.PoolableObjectFactory;

public class TestFactory implements PoolableObjectFactory{

    @Override
    public void activateObject(Object arg0) throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void destroyObject(Object arg0) throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public Object makeObject() throws Exception {
        // TODO Auto-generated method stub
        Object o=new User();
        return o;
    }

    @Override
    public void passivateObject(Object arg0) throws Exception {
        // TODO Auto-generated method stub
        
    }

    @Override
    public boolean validateObject(Object arg0) {
        // TODO Auto-generated method stub
        return false;
    }

}

package pools;

public class User {
    private String username;
    private String userpass;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getUserpass() {
        return userpass;
    }
    public void setUserpass(String userpass) {
        this.userpass = userpass;
    }
    public void print_info(){
        System.out.println(getUsername()+":"+getUserpass());
    }
}

 

参考博客 http://macrochen.iteye.com/blog/320077

 

来源:http://www.sharefly.org:8080/blog

 

你可能感兴趣的:(java)