自己搞定!谁来谁发分!
/*
* AbstractObjectPool.java
*
* http://www.javaworld.com/jw-06-1998/jw-06-object-pool.html
* http://www.codestudio.com/
* http://developer.netscape.com/viewsource/abney_pool/abney_pool.html
*
* This abstract object can be extended to implement any kind of ObjectPool.
* The goal is to reduce memory usage and enhance runtime speed by avoiding
* object creation. JVMdo
es a pretty good job with garbage collection so
* you should use this only for a bit larger objects.
*
* This implementation requires that all objects which are retrieved with
* getPoolObject() must be returned with putPoolObject() or the pool is of no
* use. It is up to the programmer to take care that this isdo
ne properly.
*
* This versiondo
esn't create any unnecessary objects like Long, Enumeration
* etc. AbstractObjectPool uses internal very light-weight Arrays for optimal
* performance.
*
* @date : 2002.3.1
* @version : 1.0.2002.03.01
* @author : Bruce Luo
*/
package com.macroview.sso.ldap;
import java.util.*;
public abstract class AbstractObjectPool
{
private Object[] data ;
// store all object instance into this array
private int nActualObject ;
// Actual Object instance in this array
private int nIncrement ;
// automatically incremented when its size becomes greater
/**
* abstract method, use to create object instance
* @return Object created Object which is always of some special type
*/
abstract Object createObject();
/**
* abstract method, Check if given Object is usable
* @return true if object is valid for use
*/
abstract boolean validate(Object obj);
/** Construct method */
public AbstractObjectPool() {
}
/** when instance this object, need init first */
public void initPool(int initialCapacity, int increment){
// Initializes the ObjectPool
data = new Object[initialCapacity];
for (int i=0;
i<initialCapacity;
i++) {
data = createObject();
}
// nClassname = data[0].getClass().getName() ;
nActualObject = initialCapacity;
nIncrement = increment;
}
/** Get a pooled object from the array. */
public synchronized Object getPoolObject() {
Object obj;
// no object instance in the object pool
if (nActualObject == 0) {
// Increases capacity of this object array
increases();
}
// get object instance reference in the bottom
obj = data[nActualObject-1];
// delete object instance reference
data[nActualObject-1] = null;
// Assign Actual object instance
nActualObject --;
return obj;
}
/** Return a pooled object to the list. */
public synchronized void putPoolObject(Object obj) {
if (validate(obj) &&
(nActualObject < capacity())) {
nActualObject ++;
// append a object instance reference to the pool array
data[nActualObject-1] = obj;
}
}
/** Increases capacity of this object array */
private synchronized void increases() {
int nLength = capacity();
// allocate new object array store the object pool
setCapacity(nLength + nIncrement);
for (int i = nActualObject;
i < nActualObject + nIncrement;
i++) {
// Creat new objects into array increase part
data = createObject();
}
// Assign Actual object instance
nActualObject += nIncrement;
}
/** Increases to special capacity of this object array */
private void setCapacity(int nCapacity)
{
if (nCapacity > capacity())
{
int nOldMaxObject = capacity();
Object[] old_data = data ;
data = new Object[nCapacity];
System.arraycopy(old_data, 0, data, 0, nOldMaxObject);
}
}
/** Returns the current capacity of this object array. */
private int capacity() {
return data.length;
}
}