lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

 

https://blog.csdn.net/tiane5hao/article/details/85957840

 

对于共享资源,有一个很著名的设计模式:资源池。该模式正是为了解决资源频繁分配、释放所造成的问题的。把该模式应用到数据库连接管理领域,就是建立一个数据库连接池,提供一套高效的连接分配、使用策略,最终目标是实现连接的高效、安全的复用。还有一类对象池大家可能不太了解,对于nio中有个堆外内存,netty的ByteBuf对象的实现就是一个可复用的对象池来提高性能。连接池大家平时听的多,使用也多,但是自己实现应该比较少。

先写一个简单通用的对象池

总共包含几个类,代码如下:
ICommonPool : 定义通用池,借出对象和归还对象
PoolFactory :对象工厂,负责创建和销毁对象
PoolConfig : 对象池的配置(最大连接数和最小连接数)
BaseCommonPool :对象池的主要基础实现

// An highlighted block
package test.pool;

/**
 * 通用池接口定义
 * @param <T>
 */
public interface ICommonPool<T> {
    /**
     * 租借对象
     */
    T borrowObject() throws Exception;

    /**
     * 返回对象池
     */
    void returnObject(T object) throws Exception;
}

package test.pool;


/**
 * 负责创建、销毁对象
 * @param <T>
 */
public interface PoolFactory<T> {

    /**
     * 创建对象
     * @return
     */
    T makeObject();

    /**
     * 销毁对象
     */
    void destroyObject();
}

 

package test.pool;

/**
 * 对象池配置
 */
public class PoolConfig {

    // 池中最大对象
    private int maxTotal = 8;
	//初始化对象数
    private int minTotal = 0;

    public int getMaxTotal() {
        return maxTotal;
    }

    public void setMaxTotal(int maxTotal) {
        this.maxTotal = maxTotal;
    }

    public int getMinTotal() {
        return minTotal;
    }

    public void setMinTotal(int minTotal) {
        this.minTotal = minTotal;
    }
}

 

 

package test.pool;

import java.util.concurrent.LinkedBlockingQueue;
/**
 * 对象池的基础实现
 * 这里用阻塞队列来实现
 * @param <T>
 */
public class BaseCommonPool<T> implements ICommonPool<T> {

    private LinkedBlockingQueue<T> pool;

    private PoolConfig poolConfig;
    private PoolFactory<T> poolFactory;

    public BaseCommonPool(PoolConfig poolConfig, PoolFactory<T> poolFactory){
        this.poolConfig = poolConfig;
        this.poolFactory = poolFactory;
        initCommonPool();
    }

    private void initCommonPool() {
    	//代表池的大小
        pool = new LinkedBlockingQueue<T>(poolConfig.getMaxTotal());
        while (poolConfig.getMinTotal() > pool.size()){
            T obj = poolFactory.makeObject();
            pool.offer(obj);
        }
    }

    /**
     * 租借对象
     */
    public T borrowObject() throws Exception{
    	//如果队列中对象为空,poll方法返回null,非阻塞(take方法阻塞)
        T obj = pool.poll();
        if(obj != null){
            return obj;
        }
        return poolFactory.makeObject();
    }

    /**
     * 返回对象池
     */
    public void returnObject(T object) throws Exception{
    	//offer方法非阻塞,如果队列满了,直接抛弃,返回false,与put方法的区别自行百度
        if(!pool.offer(object)){
            poolFactory.destroyObject();
        }
    }
}

 

通过上面的通用池实现jedis连接池

实现类如下:
MyRedisFactory : jedis的对象工厂
MyRedisPool : jedis连接池

jedis pom依赖

 <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
  </dependency>

 

 

package test.objectpool;

import redis.clients.jedis.Jedis;
import test.pool.PoolFactory;

public class MyRedisFactory implements PoolFactory<Jedis> {

    public MyRedisFactory() {
    }

    public Jedis makeObject() {
        Jedis jedis = new Jedis("127.0.0.1",6379);
        jedis.connect();
        return jedis;
    }

    public void destroyObject(Jedis jedis) {
        if (jedis.isConnected()) {
            try {
                try {
                    jedis.quit();
                } catch (Exception var4) {
                    ;
                }

                jedis.disconnect();
            } catch (Exception var5) {
                ;
            }
        }
    }

}

 

 

package test.objectpool;

import redis.clients.jedis.Jedis;
import test.pool.BaseCommonPool;
import test.pool.PoolConfig;

public class MyRedisPool extends BaseCommonPool<Jedis> {

    public MyRedisPool(PoolConfig config) {
        super( config, new MyRedisFactory());
    }
}

 

 

连接池测试

下面是jedis连接池和非连接池的测试(单线程),对于多线程后续测试了再补上,测试执行1万次本地redis方法get耗时比对结果如下:
线程池执行耗时1153 ms
创建redis执行耗时5437 ms
性能有着明显的提升

package test.objectpool;

import redis.clients.jedis.Jedis;
import test.pool.PoolConfig;

public class PoolTest {

    public static void main(String[] args) {
        PoolConfig poolConfig = new PoolConfig();
        poolConfig.setMaxTotal(8);
        poolConfig.setMinTotal(4);
        MyRedisPool myRedisPool = new MyRedisPool(poolConfig);

        //线程池执行
        long start = System.currentTimeMillis();
        for(int i = 0; i < 10000; i++){
            Jedis jedis = null;
            try {
                jedis = myRedisPool.borrowObject();
                String result = jedis.get("aaa");
//                System.out.println(result);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                myRedisPool.returnObject(jedis);
            }
        }
        System.out.println("线程池执行耗时" + (System.currentTimeMillis() - start));

        //创建redis执行
        start = System.currentTimeMillis();
        for(int i = 0; i < 10000; i++){
            Jedis jedis = null;
            try {
                jedis = new Jedis("127.0.0.1", 6379);
                String result = jedis.get("aaa");
//               System.out.println(result);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                jedis.close();
            }
        }
        System.out.println("创建redis执行耗时" + (System.currentTimeMillis() - start));
    }
}

 

posted on 2019-07-26 11:44  白露~  阅读(1095)  评论(0编辑  收藏  举报