Java 对象池 及其管理 自创

对象池的概念俺在这里就不罗嗦了,呵呵。主要是省去相同对象的频繁创建开销和回收器回收开销。

 

无依赖关系,JDK 1.5 以上的版本可以直接使用,呵呵

 

 

/**
 * @ObjectPool.java
 * @author 张光磊
 * @since  2010-3-31 下午01:41:35
 */
package cn.zgl.utils.pool;

import java.util.ArrayList;
import java.util.concurrent.Semaphore;

/**
 * @author 张光磊
 * @version 2010.03 对象池管理类
 */
public abstract  class ObjectPool<T> {
	 private ArrayList<T> pool = null;
	 private int size=0;
	 private int step=0;
	 private int currentPoolSize=0;
	 private Semaphore pass = null;
     /**
      * 初始化ObjectPool maxSize 个
      * @param maxSize
      */
     public ObjectPool(int maxSize){
 
           //初始化资源池
    	   this.size =maxSize;
           pool = new ArrayList<T>();
           for(int i=0; i<maxSize; i++){
                pool.add(createT());
           }
           currentPoolSize=maxSize;
           pass = new Semaphore(size);

     }
     /**
      * 最多初始化ObjectPool maxSize 个,默认初始化initsize 个,之后以 step递增
      * @param maxSize 最大个数
      * @param initsize 初始化个数	
      * @param step 递增个数
      */
     public ObjectPool(int maxSize,int initsize,int step){

         //初始化资源池
    	 this.size =maxSize;
    	 this.step=step;
         pool = new ArrayList<T>();
         for(int i=0; i<maxSize; i++){
        	 pool.add(createT());
         }
         currentPoolSize=maxSize;
         pass = new Semaphore(size);

     }
     /**
      * 从池中读取一个
      * @author 张光磊
      * @since  2010-3-31 下午04:21:19
      * @return T
      * @return
      * @throws InterruptedException
      */
     public T get() throws InterruptedException{

           //获取通行证,只有得到通行证后才能得到资源
           pass.acquire();
           return getSourceFromPool();

     }
     /**
      * 归还一个到池中
      * @author 张光磊
      * @since  2010-3-31 下午04:21:30
      * @return void
      * @param resource
      */
     public void put(T resource){

           //归还通行证,并归还资源
           pass.release();
           releaseSourceToPool(resource);

     }
     /**
      * 将池中的全部释放
      * @author 张光磊
      * @since  2010-3-31 下午04:21:41
      * @return void
      */
    //public void relasePool(){
    //	 for(T t:pool){
    //		 destroyT(t);
    //	 }
    //}
    //更正为   
     public void relasePool(){
    	 for(int i=0;i<size;i++){
                       pass.acquire();
    	      destroyT(getSourceFromPool());
    	 }
     }
  /**
     * 读取一个 私有
     * @author 张光磊
     * @since  2010-3-31 下午04:22:17
     * @return T
     * @return
     */
    private synchronized T getSourceFromPool() {
        if(pool.size()==0){
     	   if(currentPoolSize<size){
     		  int newT= size-currentPoolSize>step ? step: size-currentPoolSize; 
     		  for(int i=0; i<newT; i++){
                  pool.add(createT());
             }
     		 currentPoolSize+=newT;
     	   }
        }
    	return pool.remove(0);
     }
     /**
      * 释放一个 到 池中 私有
      * @author 张光磊
      * @since  2010-3-31 下午04:22:34
      * @return void
      * @param resource
      */
     private synchronized void releaseSourceToPool(T resource) {
          // System.out.println("return "+resource);
          pool.add(resource);
     } 
     
     //创建、销毁对象
   abstract T createT();
     abstract void destroyT(T t);
     // 示例:public class IntegerPool extends ObjectPool<Integer>
}

下面是一个简单的示例,呵呵

 

/**
 * @IntegerPoolExample.java
 * @author 张光磊
 * @since  2010-3-31 下午03:18:05
 */
package cn.zgl.utils.pool;

/**
 * @author 张光磊
 */
public class IntegerPoolExample extends ObjectPool<Integer>{

	/**
	 * @param maxSize
	 */
	public IntegerPoolExample(int maxSize) {
		super(maxSize);
		// TODO Auto-generated constructor stub
	}
	
	public IntegerPoolExample() {
		super(10);
	}
	/* (non-Javadoc)
	 * @see cn.zgl.utils.pool.ObjectPool#createT()
	 */
	@Override
	Integer createT() {
		return 0;
	}

	/* (non-Javadoc)
	 * @see cn.zgl.utils.pool.ObjectPool#destroyT(java.lang.Object)
	 */
	@Override
	void destroyT(Integer t) {
		t=null;
	}
}

 

 

当然 Integer 一般是不需要用池来管理的,这仅仅是一个示例,呵呵。

 

  使用示例

 

public static void main(String[] args){
		ObjectPool<Integer> pool= new IntegerPoolExample();
		try{
			for(int i=0;i<10;i++){
				//从池中取出一个
				Integer tmp=pool.get();
				System.out.println("第"+i+"次输出内容 " +tmp);
				//修改取出的这个值
				tmp=i;
				//把修改后的值访问去 这个地方只是测试测试,实际上池中对象一般要求使用前、使用后没有变化
				pool.put(tmp);
			}
			for(int i=0;i<10;i++){
				System.out.println("第"+i+"次输出内容 " +pool.get());
			}
		}catch(InterruptedException e){
			e.printStackTrace();
		}
		finally{
			pool.relasePool();
		}

	}

 

 

 

那么,到此为止了。呵呵,大家多多指点,个人资料请勿随意转载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值