1 17 18 package org.apache.tomcat.util.collections; 19 20 28 public final class SimplePool { 29 30 31 private static org.apache.commons.logging.Log log= 32 org.apache.commons.logging.LogFactory.getLog(SimplePool.class ); 33 34 37 private Object pool[]; 38 39 private int max; 40 private int last; 41 private int current=-1; 42 43 private Object lock; 44 public static final int DEFAULT_SIZE=32; 45 static final int debug=0; 46 47 public SimplePool() { 48 this(DEFAULT_SIZE,DEFAULT_SIZE); 49 } 50 51 public SimplePool(int size) { 52 this(size, size); 53 } 54 55 public SimplePool(int size, int max) { 56 this.max=max; 57 pool=new Object [size]; 58 this.last=size-1; 59 lock=new Object (); 60 } 61 62 public void set(Object o) { 63 put(o); 64 } 65 66 69 public void put(Object o) { 70 synchronized( lock ) { 71 if( current < last ) { 72 current++; 73 pool[current] = o; 74 } else if( current < max ) { 75 int newSize=pool.length*2; 77 if( newSize > max ) newSize=max+1; 78 Object tmp[]=new Object [newSize]; 79 last=newSize-1; 80 System.arraycopy( pool, 0, tmp, 0, pool.length); 81 pool=tmp; 82 current++; 83 pool[current] = o; 84 } 85 if( debug > 0 ) log("put " + o + " " + current + " " + max ); 86 } 87 } 88 89 92 public Object get() { 93 Object item = null; 94 synchronized( lock ) { 95 if( current >= 0 ) { 96 item = pool[current]; 97 pool[current] = null; 98 current -= 1; 99 } 100 if( debug > 0 ) 101 log("get " + item + " " + current + " " + max); 102 } 103 return item; 104 } 105 106 109 public int getMax() { 110 return max; 111 } 112 113 116 public int getCount() { 117 return current+1; 118 } 119 120 121 public void shutdown() { 122 } 123 124 private void log( String s ) { 125 if (log.isDebugEnabled()) 126 log.debug("SimplePool: " + s ); 127 } 128 } 129 | Popular Tags |