1 11 package org.jboss.portlet.forums.helper; 12 13 14 21 public abstract class AbstractPool 22 { 23 25 26 private final Object lock = new Object (); 27 28 29 private Object [] stack; 30 31 32 private int index; 33 34 protected AbstractPool() 36 { 37 } 38 39 41 47 public Object acquire() 48 { 49 Object instance = null; 51 52 synchronized (lock) 54 { 55 if (index > -1) 56 { 57 instance = stack[index]; 58 stack[index--] = null; 59 } 60 } 61 62 if (instance == null) 64 { 65 instance = _create(); 66 } 67 68 _acquire(instance); 70 71 return instance; 73 } 74 75 81 public void release(Object instance) 82 { 83 if (instance == null) 84 { 85 return; 88 } 89 90 _release(instance); 92 93 synchronized (lock) 95 { 96 if (index < (stack.length - 1)) 97 { 98 stack[++index] = instance; 99 instance = null; 100 } 101 } 102 103 if (instance != null) 106 { 107 _destroy(instance); 108 } 109 } 110 111 113 121 protected void initialize(int maxSize, 122 int initialSize) 123 { 124 if (maxSize < 1) 126 { 127 throw new IllegalArgumentException ("maxSize must be greater than 0"); 128 } 129 130 initialSize = (initialSize < 0) ? 0 : initialSize; 131 initialSize = (initialSize > maxSize) ? maxSize : initialSize; 132 133 stack = new Object [maxSize]; 135 this.index = -1; 136 while (initialSize-- > 0) 137 { 138 stack[++index] = _create(); 139 } 140 } 141 142 146 protected void _destroy(Object instance) 147 { 148 } 149 150 154 protected void _acquire(Object instance) 155 { 156 } 157 158 162 protected void _release(Object instance) 163 { 164 } 165 166 168 171 protected abstract Object _create(); 172 } | Popular Tags |