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

相关文章

  • java access modifiers public protected friendly private usage summary 2009-04-01

    First, the statement: java in, friendly, and this modifier is not an explicit statement of the member variables and methods of the former would not have any modifiers, the default is friendly. For clarity, sub-summed up in three different circumstanc

  • java where the difference between abstract classes and interfaces 2010-11-04

    java where the difference between abstract classes and interfaces This article is the following very thorough, it reproduced the abstract class and interface is an abstract class for the Java language to support the definition of the two mechanisms,

  • Java接口Interface与抽象类Abstract Class 2010-12-29

    一. 面向接口编程 在一个面向对象的系统中,系统的各种功能是由许许多多的不同对象协作完成的.在这种情况下,各个对象内部是如何实现自己的对系统设计人员来讲就不 那么重要了:而各个对象之间的协作关系则成为系统设计的关键.小到不同类之间的通信,大到各模块之间的交互,在系统设计之初都是要着重考虑的,这也是系统 设计的主要工作内容.面向接口编程我想就是指按照这种思想来编程吧!实际上,在日常工作中,你已经按照接口编程了,只不过如果你没有这方面的意识,那么你 只是在被动的实现这一思想:表现在频繁的抱怨别人改的

  • Java socket - an example with a connection pool 2010-04-12

    An example with a connection pool 1. Introduction We now have the MultithreadedServer whenever a client to apply for a connection in a new Thread create a new ConnectionHandler. This means that there may be a bunch of Thread "lying" around us. A

  • Comparison of the size of objects in java 2010-06-10

    Comparison of the size of objects in java Comparing the size of java objects There are two ways 1: Implement Comparable interface public int compareTo (T o) method; 2: Implement Comparator interface int compare (T o1, T o2) method; Code: import java.

  • java socket programming collection of four (thread pool) 2010-06-16

    1) Server-side Java code import java.io. *; import java.net .*; public class PooledRemoteFileServer ( / / Server can deal with the activities of the maximum number of client connections protected int maxConnections; / / Listening port for incoming co

  • ajax using dwr tools to pass the value from the front page to back Java objects inside, and then according to the results of the object to return to the page up (back Qinbo personal summary) 2010-03-18

    ajax using dwr tools, from the front page the value passed to the Java objects back inside, then according to the results of the object to return to the page up (back Qinbo personal summary) Edit Article Categories: Web front-end 1.java class public

  • Java reflection of: access to an array of the type of object components 2010-12-24

    public class Main { public static void main(String[] argv) { System.out.println(new int[1][2][3].getClass().getComponentType()); } }

  • java in the interfaces and abstract classes 2010-03-22

    JAVA in the abstract class can be inherited inside an abstract class can have common methods, they can have abstract methods (no method body of the abstract method, there is no braces () normal method can have a method body) If an ordinary class inhe

  • Java's interface and the abstract class to another 2010-03-14

    Java's interface and abstract class can not provide concrete realization of the definition of the method, their difference is: 1. A class can not inherit from multiple abstract class, but can implement multiple interface; 2. From the language level o

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

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

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

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