SimplePoolpublic 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. |
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.Object | get()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 int | getCount()Number of object in the pool
return current+1;
| public int | getMax()Return the size of the pool
return max;
| private void | log(java.lang.String s)
if (log.isDebugEnabled())
log.debug("SimplePool: " + s );
| public void | put(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 void | set(java.lang.Object o)
put(o);
| public void | shutdown()
|
|