1 55 56 package org.jboss.axis.components.threadpool; 57 58 import org.jboss.axis.i18n.Messages; 59 import org.jboss.logging.Logger; 60 61 import java.util.Hashtable ; 62 import java.util.Iterator ; 63 import java.util.Map ; 64 65 68 public class ThreadPool 69 { 70 71 private static Logger log = Logger.getLogger(ThreadPool.class.getName()); 72 73 public static final long MAX_THREADS = 100; 74 75 protected Map threads = new Hashtable (); 76 protected long threadcount; 77 public boolean _shutdown; 78 79 public void cleanup() 80 throws InterruptedException 81 { 82 if (log.isDebugEnabled()) 83 { 84 log.debug("Enter: ThreadPool::cleanup"); 85 } 86 if (!isShutdown()) 87 { 88 safeShutdown(); 89 awaitShutdown(); 90 } 91 synchronized (this) 92 { 93 threads.clear(); 94 _shutdown = false; 95 } 96 if (log.isDebugEnabled()) 97 { 98 log.debug("Exit: ThreadPool::cleanup"); 99 } 100 } 101 102 105 public boolean isShutdown() 106 { 107 synchronized (this) 108 { 109 return _shutdown && threadcount == 0; 110 } 111 } 112 113 116 public boolean isShuttingDown() 117 { 118 synchronized (this) 119 { 120 return _shutdown; 121 } 122 } 123 124 127 public long getWorkerCount() 128 { 129 synchronized (this) 130 { 131 return threadcount; 132 } 133 } 134 135 138 public void addWorker(Runnable worker) 139 { 140 if (log.isDebugEnabled()) 141 { 142 log.debug("Enter: ThreadPool::addWorker"); 143 } 144 if (_shutdown || 145 threadcount == MAX_THREADS) 146 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 147 Thread thread = new Thread (worker); 148 threads.put(worker, thread); 149 threadcount++; 150 thread.start(); 151 if (log.isDebugEnabled()) 152 { 153 log.debug("Exit: ThreadPool::addWorker"); 154 } 155 } 156 157 160 public void interruptAll() 161 { 162 if (log.isDebugEnabled()) 163 { 164 log.debug("Enter: ThreadPool::interruptAll"); 165 } 166 synchronized (threads) 167 { 168 for (Iterator i = threads.values().iterator(); i.hasNext();) 169 { 170 Thread t = (Thread )i.next(); 171 t.interrupt(); 172 } 173 } 174 if (log.isDebugEnabled()) 175 { 176 log.debug("Exit: ThreadPool::interruptAll"); 177 } 178 } 179 180 183 public void shutdown() 184 { 185 if (log.isDebugEnabled()) 186 { 187 log.debug("Enter: ThreadPool::shutdown"); 188 } 189 synchronized (this) 190 { 191 _shutdown = true; 192 } 193 interruptAll(); 194 if (log.isDebugEnabled()) 195 { 196 log.debug("Exit: ThreadPool::shutdown"); 197 } 198 } 199 200 203 public void safeShutdown() 204 { 205 if (log.isDebugEnabled()) 206 { 207 log.debug("Enter: ThreadPool::safeShutdown"); 208 } 209 synchronized (this) 210 { 211 _shutdown = true; 212 } 213 if (log.isDebugEnabled()) 214 { 215 log.debug("Exit: ThreadPool::safeShutdown"); 216 } 217 } 218 219 222 public synchronized void awaitShutdown() 223 throws InterruptedException 224 { 225 if (log.isDebugEnabled()) 226 { 227 log.debug("Enter: ThreadPool::awaitShutdown"); 228 } 229 if (!_shutdown) 230 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 231 while (threadcount > 0) 232 wait(); 233 if (log.isDebugEnabled()) 234 { 235 log.debug("Exit: ThreadPool::awaitShutdown"); 236 } 237 } 238 239 242 public synchronized boolean awaitShutdown(long timeout) 243 throws InterruptedException 244 { 245 if (log.isDebugEnabled()) 246 { 247 log.debug("Enter: ThreadPool::awaitShutdown"); 248 } 249 if (!_shutdown) 250 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 251 if (threadcount == 0) 252 { 253 if (log.isDebugEnabled()) 254 { 255 log.debug("Exit: ThreadPool::awaitShutdown"); 256 } 257 return true; 258 } 259 long waittime = timeout; 260 if (waittime <= 0) 261 { 262 if (log.isDebugEnabled()) 263 { 264 log.debug("Exit: ThreadPool::awaitShutdown"); 265 } 266 return false; 267 } 268 long start = System.currentTimeMillis(); 269 for (; ;) 270 { 271 wait(waittime); 272 if (threadcount == 0) 273 { 274 if (log.isDebugEnabled()) 275 { 276 log.debug("Exit: ThreadPool::awaitShutdown"); 277 } 278 return true; 279 } 280 waittime = timeout - System.currentTimeMillis(); 281 if (waittime <= 0) 282 { 283 if (log.isDebugEnabled()) 284 { 285 log.debug("Exit: ThreadPool::awaitShutdown"); 286 } 287 return false; 288 } 289 } 290 } 291 292 295 public void workerDone(Runnable worker, 296 boolean restart) 297 { 298 if (log.isDebugEnabled()) 299 { 300 log.debug("Enter: ThreadPool::workerDone"); 301 } 302 synchronized (this) 303 { 304 threads.remove(worker); 305 if (--threadcount == 0 && _shutdown) 306 { 307 notifyAll(); 308 } 309 if (!_shutdown && restart) 310 { 311 addWorker(worker); 312 } 313 } 314 if (log.isDebugEnabled()) 315 { 316 log.debug("Exit: ThreadPool::workerDone"); 317 } 318 } 319 } 320 321 | Popular Tags |