FileDocCategorySizeDatePackage
SimplePool.javaAPI DocApache Tomcat 6.0.143181Fri Jul 20 04:20:36 BST 2007org.apache.tomcat.util.collections

SimplePool

public final class SimplePool extends Object
Simple object pool. Based on ThreadPool and few other classes The pool will ignore overflow and return null if empty.
author
Gal Shachor
author
Costin Manolache

Fields Summary
private static org.apache.juli.logging.Log
log
private Object[]
pool
private int
max
private int
last
private int
current
private Object
lock
public static final int
DEFAULT_SIZE
static final int
debug
Constructors Summary
public SimplePool()

    
      
	this(DEFAULT_SIZE,DEFAULT_SIZE);
    
public SimplePool(int size)

	this(size, size);
    
public SimplePool(int size, int max)

	this.max=max;
	pool=new Object[size];
	this.last=size-1;
	lock=new Object();
    
Methods Summary
public java.lang.Objectget()
Get an object from the pool, null if the pool is empty.

	Object item = null;
	synchronized( lock ) {
	    if( current >= 0 ) {
		item = pool[current];
		pool[current] = null;
		current -= 1;
	    }
	    if( debug > 0 ) 
		log("get " + item + " " + current + " " + max);
	}
	return item;
    
public intgetCount()
Number of object in the pool

	return current+1;
    
public intgetMax()
Return the size of the pool

	return max;
    
private voidlog(java.lang.String s)

        if (log.isDebugEnabled())
            log.debug("SimplePool: " + s );
    
public voidput(java.lang.Object o)
Add the object to the pool, silent nothing if the pool is full

	synchronized( lock ) {
	    if( current < last ) {
		current++;
		pool[current] = o;
            } else if( current < max ) {
		// realocate
		int newSize=pool.length*2;
		if( newSize > max ) newSize=max+1;
		Object tmp[]=new Object[newSize];
		last=newSize-1;
		System.arraycopy( pool, 0, tmp, 0, pool.length);
		pool=tmp;
		current++;
		pool[current] = o;
	    }
	    if( debug > 0 ) log("put " + o + " " + current + " " + max );
	}
    
public voidset(java.lang.Object o)

	put(o);
    
public voidshutdown()