commons-pool-1.6.0对象池技术

1.6和1.5最大的区别是支持泛型,编程比较方便

池的大小是初始化配置的,
在没有显式配置指定的情况下,
池大小默认为8.
可以通过GenericKeyedObjectPool.Config来配置。

下面给出2个例子,大家可以自行运行,或者跟贴留言,我来解答

根据key获取对象
package com.panguso.phl;

import org.apache.commons.pool.BaseKeyedPoolableObjectFactory;
import org.apache.commons.pool.impl.GenericKeyedObjectPool;

/**
1、复杂对象应该实现equals() 、hashCode()方法,以便不重复入池。
2、试图将null入池,不会成功的,但也不抛异常。
3、对于池对象,使用应该遵循“接而有还,再借不难”-----从池中获取,不用时候归池,否则当池中没有空闲可用的对象时候,程序会处于等待状态,如果没有显式的调用归还方法,则造成程序死锁。
 * @author piaohailin
 *
 */
public class TestGenericKeyedObjectPool {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        //创建一个对象池 
        GenericKeyedObjectPool<Integer, String> pool = new GenericKeyedObjectPool<Integer, String>(new BaseKeyedPoolableObjectFactory<Integer, String>() {
            @Override
            public String makeObject(Integer o) throws Exception {
                return String.valueOf(o);
            }
        });

        //清除最早的对象 
        pool.clearOldest();

        //添加对象到池,重复的不会重复入池 
        for (int i = 0; i < 5; i++) {
            pool.addObject(i);
        }

        //获取并输出对象 
        for (int i = 0; i < 10; i++) {
            System.out.println(pool.borrowObject(i));
        }
        System.out.println("==========");
        //输出池状态 
        System.out.println("pool.getMaxIdle()=" + pool.getMaxIdle());
        System.out.println("pool.getMaxActive()=" + pool.getMaxActive());

    }

}


无参获取对象
package com.panguso.phl;

import java.util.Random;

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;
import org.apache.commons.pool.impl.GenericObjectPool.Config;

public class TestGenericObjectPool {

    public static void main(String[] args) throws Exception {
        Config config = new Config();
        config.maxActive = 100;
        config.maxWait = 3000; //设置这个,会抛Exception in thread "main" java.util.NoSuchElementException: Timeout waiting for idle object
        GenericObjectPool<Float> pool = new GenericObjectPool<Float>(new BasePoolableObjectFactory<Float>() {
            public Float makeObject() throws Exception {
                return new Random().nextFloat();
            }
        }, config);

//        for (int i = 0; i < 10000; i++) {
//            pool.addObject();
//        }
        for (int i = 0; i < 10000; i++) {
            System.out.println(System.currentTimeMillis());
            Float f = pool.borrowObject();
            System.out.println(f);
            System.out.println(System.currentTimeMillis());
        }

        Float f = pool.borrowObject();
        System.out.println(f);
        pool.returnObject(f);

        Float o1 = pool.borrowObject();
        System.out.println(o1);

        Float o2 = pool.borrowObject();
        System.out.println(o2);

        Float o3 = pool.borrowObject();
        System.out.println(o3);

        Float o4 = pool.borrowObject();
        System.out.println(o4);

        Float o5 = pool.borrowObject();
        System.out.println(o5);

        Float o6 = pool.borrowObject();
        System.out.println(o6);

        Float o7 = pool.borrowObject();
        System.out.println(o7);

        Float o8 = pool.borrowObject();
        System.out.println(o8);

        pool.returnObject(o1);
        pool.returnObject(o2);
        pool.returnObject(o3);
        pool.returnObject(o4);
        pool.returnObject(o5);
        pool.returnObject(o6);
        pool.returnObject(o7);
        pool.returnObject(o8);

        Float o9 = pool.borrowObject();
        System.out.println(o9);
        pool.returnObject(o9);

        Float o10 = pool.borrowObject();
        System.out.println(o10);
        pool.returnObject(o10);

        Float o11 = pool.borrowObject();
        System.out.println(o11);
        pool.returnObject(o11);

        Float o12 = pool.borrowObject();
        System.out.println(o12);
        pool.returnObject(o12);

        Float o13 = pool.borrowObject();
        System.out.println(o13);
        pool.returnObject(o13);

    }
}

你可能感兴趣的:(commons)