Java object pooling and management of own

The concept of object pooling is not wordy here I was, huh, huh. Largely eliminating the need for frequent the same object to create overhead and recovery overhead collector.

No dependencies, JDK 1.5 or later can be used directly, huh, huh

/**
 * @ObjectPool.java
 * @author  Zhang Lei
 * @since  2010-3-31  Afternoon  01:41:35
 */
package cn.zgl.utils.pool;

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

/**
 * @author  Zhang Lei
 * @version 2010.03  Object pool management class
 */
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;
     /**
      *  Initializes a maxSize ObjectPool
      * @param maxSize
      */
     public ObjectPool(int maxSize){

           // Initialize resource pool
           this.size =maxSize;
           pool = new ArrayList<T>();
           for(int i=0; i<maxSize; i++){
                pool.add(createT());
           }
           currentPoolSize=maxSize;
           pass = new Semaphore(size);

     }
     /**
      *  Up to initialize a maxSize ObjectPool  , The default initialization initsize than  , After taking the step increment
      * @param maxSize  The maximum number
      * @param initsize  Initializes the number
      * @param step  Increment the number
      */
     public ObjectPool(int maxSize,int initsize,int step){

         // Initialize resource pool
         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);

     }
     /**
      *  Read one from the pool
      * @author  Zhang Lei
      * @since  2010-3-31  Afternoon  04:21:19
      * @return T
      * @return
      * @throws InterruptedException
      */
     public T get() throws InterruptedException{

           // Gets a pass, only get passes to get resource
           pass.acquire();
           return getSourceFromPool();

     }
     /**
      *  Return a to pool
      * @author  Zhang Lei
      * @since  2010-3-31  Afternoon  04:21:30
      * @return void
      * @param resource
      */
     public void put(T resource){

           // The return of the pass, and the return of resources
           pass.release();
           releaseSourceToPool(resource);

     }
     /**
      *  All releases of the pool
      * @author  Zhang Lei
      * @since  2010-3-31  Afternoon  04:21:41
      * @return void
      */
     public void relasePool(){
         for(T t:pool){
                 destroyT(t);
         }
     }
    /**
     *  Read a private
     * @author  Zhang Lei
     * @since  2010-3-31  Afternoon  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);
     }
     /**
      *  Release one to    Private pool
      * @author  Zhang Lei
      * @since  2010-3-31  Afternoon  04:22:34
      * @return void
      * @param resource
      */
     private synchronized void releaseSourceToPool(T resource) {
          // System.out.println("return "+resource);
          pool.add(resource);
     } 

     // Destroy the object was created.
   abstract T createT();
     abstract void destroyT(T t);
     //  Sample  :public class IntegerPool extends ObjectPool<Integer>
}

Here is a simple example, Oh

/**
 * @IntegerPoolExample.java
 * @author  Zhang Lei
 * @since  2010-3-31  Afternoon  03:18:05
 */
package cn.zgl.utils.pool;

/**
 * @author  Zhang Lei
 */
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;
        }
}

Of course, generally do not need to use Integer to manage the pool, and this is just an example, huh, huh.

Use the sample

public static void main(String[] args){
                ObjectPool<Integer> pool= new IntegerPoolExample();
                try{
                        for(int i=0;i<10;i++){
                                // Drawn from the pool a
                                Integer tmp=pool.get();
                                System.out.println(" The first "+ I +" time output   " +tmp);
                                // Modify the value of this
                                tmp=i;
                                // The modified value access to this place is just a test test  , In fact the pool object General requirements before using no change after use.
                                pool.put(tmp);
                        }
                        for(int i=0;i<10;i++){
                                System.out.println(" The first "+ I +" time output   " +pool.get());
                        }
                }catch(InterruptedException e){
                        e.printStackTrace();
                }
                finally{
                        pool.relasePool();
                }

        }

So, stop here. Oh, you lot of pointing, personal information Please do not reproduced

分类:Java 时间:2010-03-31 人气:188
分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.561 (s). 12 q(s)