1 16 17 package org.apache.axis.components.threadpool; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.i18n.Messages; 21 import org.apache.commons.logging.Log; 22 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.Map ; 26 27 30 public class ThreadPool { 31 32 protected static Log log = 33 LogFactory.getLog(ThreadPool.class.getName()); 34 35 public static final int DEFAULT_MAX_THREADS = 100; 36 37 protected Map threads = new Hashtable (); 38 protected long threadcount; 39 public boolean _shutdown; 40 private int maxPoolSize = DEFAULT_MAX_THREADS; 41 42 public ThreadPool() { 43 } 44 45 public ThreadPool(int maxPoolSize) { 46 this.maxPoolSize = maxPoolSize; 47 } 48 49 public void cleanup() 50 throws InterruptedException { 51 if (log.isDebugEnabled()) { 52 log.debug("Enter: ThreadPool::cleanup"); 53 } 54 if (!isShutdown()) { 55 safeShutdown(); 56 awaitShutdown(); 57 } 58 synchronized(this) { 59 threads.clear(); 60 _shutdown = false; 61 } 62 if (log.isDebugEnabled()) { 63 log.debug("Exit: ThreadPool::cleanup"); 64 } 65 } 66 67 70 public boolean isShutdown() { 71 synchronized (this) { 72 return _shutdown && threadcount == 0; 73 } 74 } 75 76 79 public boolean isShuttingDown() { 80 synchronized (this) { 81 return _shutdown; 82 } 83 } 84 85 88 public long getWorkerCount() { 89 synchronized (this) { 90 return threadcount; 91 } 92 } 93 94 97 public void addWorker( 98 Runnable worker) { 99 if (log.isDebugEnabled()) { 100 log.debug("Enter: ThreadPool::addWorker"); 101 } 102 if (_shutdown || threadcount == maxPoolSize) { 103 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 104 } 105 Thread thread = new Thread (worker); 106 threads.put(worker, thread); 107 threadcount++; 108 thread.start(); 109 if (log.isDebugEnabled()) { 110 log.debug("Exit: ThreadPool::addWorker"); 111 } 112 } 113 114 117 public void interruptAll() { 118 if (log.isDebugEnabled()) { 119 log.debug("Enter: ThreadPool::interruptAll"); 120 } 121 synchronized (threads) { 122 for (Iterator i = threads.values().iterator(); i.hasNext();) { 123 Thread t = (Thread ) i.next(); 124 t.interrupt(); 125 } 126 } 127 if (log.isDebugEnabled()) { 128 log.debug("Exit: ThreadPool::interruptAll"); 129 } 130 } 131 132 135 public void shutdown() { 136 if (log.isDebugEnabled()) { 137 log.debug("Enter: ThreadPool::shutdown"); 138 } 139 synchronized (this) { 140 _shutdown = true; 141 } 142 interruptAll(); 143 if (log.isDebugEnabled()) { 144 log.debug("Exit: ThreadPool::shutdown"); 145 } 146 } 147 148 151 public void safeShutdown() { 152 if (log.isDebugEnabled()) { 153 log.debug("Enter: ThreadPool::safeShutdown"); 154 } 155 synchronized (this) { 156 _shutdown = true; 157 } 158 if (log.isDebugEnabled()) { 159 log.debug("Exit: ThreadPool::safeShutdown"); 160 } 161 } 162 163 166 public synchronized void awaitShutdown() 167 throws InterruptedException { 168 if (log.isDebugEnabled()) { 169 log.debug("Enter: ThreadPool::awaitShutdown"); 170 } 171 if (!_shutdown) 172 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 173 while (threadcount > 0) 174 wait(); 175 if (log.isDebugEnabled()) { 176 log.debug("Exit: ThreadPool::awaitShutdown"); 177 } 178 } 179 180 183 public synchronized boolean awaitShutdown(long timeout) 184 throws InterruptedException { 185 if (log.isDebugEnabled()) { 186 log.debug("Enter: ThreadPool::awaitShutdown"); 187 } 188 if (!_shutdown) 189 throw new IllegalStateException (Messages.getMessage("illegalStateException00")); 190 if (threadcount == 0) { 191 if (log.isDebugEnabled()) { 192 log.debug("Exit: ThreadPool::awaitShutdown"); 193 } 194 return true; 195 } 196 long waittime = timeout; 197 if (waittime <= 0) { 198 if (log.isDebugEnabled()) { 199 log.debug("Exit: ThreadPool::awaitShutdown"); 200 } 201 return false; 202 } 203 for (; ;) { 204 wait(waittime); 205 if (threadcount == 0) { 206 if (log.isDebugEnabled()) { 207 log.debug("Exit: ThreadPool::awaitShutdown"); 208 } 209 return true; 210 } 211 waittime = timeout - System.currentTimeMillis(); 212 if (waittime <= 0) { 213 if (log.isDebugEnabled()) { 214 log.debug("Exit: ThreadPool::awaitShutdown"); 215 } 216 return false; 217 } 218 } 219 } 220 221 224 public void workerDone( 225 Runnable worker, 226 boolean restart) { 227 if (log.isDebugEnabled()) { 228 log.debug("Enter: ThreadPool::workerDone"); 229 } 230 synchronized(this) { 231 threads.remove(worker); 232 if (--threadcount == 0 && _shutdown) { 233 notifyAll(); 234 } 235 if (!_shutdown && restart) { 236 addWorker(worker); 237 } 238 } 239 if (log.isDebugEnabled()) { 240 log.debug("Exit: ThreadPool::workerDone"); 241 } 242 } 243 } 244 245 | Popular Tags |