1 23 24 29 42 43 48 49 package com.sun.enterprise.util.threadpool; 50 51 import java.util.Properties ; 52 53 import com.sun.enterprise.util.threadpool.Servicable; 54 import com.sun.enterprise.util.collection.BlockingQueue; 55 import com.sun.enterprise.util.collection.QueueClosedException; 56 import com.sun.enterprise.util.collection.TooManyTasksException; 57 import com.sun.enterprise.util.pool.Pool; 58 import com.sun.enterprise.util.pool.AbstractPool; 59 import com.sun.enterprise.util.pool.BoundedPool; 60 import com.sun.enterprise.util.pool.ObjectFactory; 61 62 import com.sun.enterprise.util.collection.DListNode; 63 import java.util.logging.Logger ; 65 import java.util.logging.Level ; 66 import com.sun.logging.LogDomains; 67 69 75 public class FastThreadPool { 76 77 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 79 protected boolean bDebug = false; 81 private PoolProperties poolProps; 82 private TaskQueue taskQueue; 83 private int waitCount = 0; 84 private int totalThreadCreatedCount = 0; private int totalThreadDestroyedCount = 0; private int numMessages = 0; 87 88 public FastThreadPool(String threadGroupName, int minThreadCount, int maxThreadCount, 89 long maxIdleTime, int queueLimit, TaskFactory factory) { 90 this(new ThreadGroup (threadGroupName), minThreadCount, maxThreadCount, 91 maxIdleTime, new TaskQueue(queueLimit, factory)); 92 } 93 94 public FastThreadPool(ThreadGroup threadGroup, int minThreadCount, int maxThreadCount, 95 long maxIdleTime, int queueLimit, TaskFactory factory) { 96 this(threadGroup, minThreadCount, maxThreadCount, maxIdleTime, 97 new TaskQueue(queueLimit, factory)); 98 } 99 100 public FastThreadPool(ThreadGroup threadGroup, int minThreadCount, int maxThreadCount, 101 long maxIdleTime, TaskQueue queue) { 102 this.taskQueue = queue; 103 104 poolProps = new PoolProperties(minThreadCount, maxThreadCount, 105 maxIdleTime, taskQueue, threadGroup); 106 107 } 108 109 113 public void start() { 114 poolProps.createdCount = poolProps.minThreadCount; 116 for (int i=0; i < poolProps.minThreadCount; i++) { 117 new ThreadPoolThread(poolProps); 123 } 124 com.sun.enterprise.util.MonitorTask.addORBMonitorable(this); 126 } 128 129 132 public TaskQueue getTaskQueue() { 133 return taskQueue; 134 } 135 136 139 public boolean setTaskQueue(TaskQueue bq) { 140 if (taskQueue != null) 141 return false; 142 taskQueue = bq; 143 return true; 144 } 145 146 149 public void addFirst(Servicable servicable) 150 throws TooManyTasksException, QueueClosedException { 151 taskQueue.addFirst(servicable); 152 } 153 154 157 public void addLast(Servicable servicable) 158 throws TooManyTasksException, QueueClosedException { 159 taskQueue.addLast(servicable); 160 } 161 162 165 public void add(int index, Servicable servicable) 166 throws TooManyTasksException, QueueClosedException { 167 taskQueue.add(index, servicable); 168 } 169 170 public void shutdown() { 171 taskQueue.shutdown(); 172 } 173 174 public void abort() { 175 taskQueue.abort(); 176 } 177 178 public int getPoolSize() { 179 return (poolProps != null) ? (poolProps.createdCount) : -1; 180 } 181 182 public int getWaitCount() { 183 return waitCount; 184 } 185 186 public int[] getMonitoredValues() { 187 synchronized(poolProps) { 188 int [] ret = {(poolProps != null) ? (poolProps.createdCount) : -1, 190 waitCount}; 191 return ret; 192 } 193 } 194 195 197 200 public String toString() { 201 StringBuffer sb = new StringBuffer (); 202 sb.append("FastThreadPool [CS=").append(poolProps.createdCount); 203 sb.append(", TC=").append(totalThreadCreatedCount); 204 sb.append(", TD=").append(totalThreadDestroyedCount); 205 sb.append(", Min=").append(poolProps.minThreadCount); 206 sb.append(", Max=").append(poolProps.maxThreadCount); 207 sb.append(", MaxIdle=").append(poolProps.maxIdleTime); 208 sb.append(", Msgs=").append(numMessages); 209 sb.append("]"); 210 return sb.toString(); 211 } 212 213 215 private class PoolProperties { 216 int minThreadCount; 217 int maxThreadCount; 218 long maxIdleTime; 219 TaskQueue taskQueue; 220 ThreadGroup threadGroup; 221 222 int createdCount; 223 224 PoolProperties(int minThreadCount, int maxThreadCount, long maxIdleTime, 225 TaskQueue taskQueue, ThreadGroup threadGroup) { 226 this.minThreadCount = minThreadCount; 227 this.maxThreadCount = maxThreadCount; 228 this.maxIdleTime = maxIdleTime; 229 this.taskQueue = taskQueue; 230 this.threadGroup = threadGroup; 231 232 this.createdCount = 0; 233 } 234 } 235 236 private class ThreadPoolThread implements Runnable { 237 PoolProperties poolProps; 238 239 ThreadPoolThread(PoolProperties poolProps) { 240 this.poolProps = poolProps; 241 Thread thread = new Thread (poolProps.threadGroup, this); 242 if (poolProps.threadGroup != null) { 243 if (poolProps.threadGroup.isDaemon()) { 244 thread.setDaemon(true); 245 } 246 } 247 thread.start(); 248 totalThreadCreatedCount++; } 250 251 public void run() { 252 Servicable task = null; 253 try { 254 while (true) { 255 boolean canCreateBuddy = false; 256 257 do { 258 synchronized (poolProps) { 259 waitCount++; 260 } 261 262 task = null; 264 task = (Servicable) taskQueue.remove(poolProps.maxIdleTime); 265 synchronized (poolProps) { 266 waitCount--; 267 if (task == null) { 268 if (poolProps.createdCount > poolProps.minThreadCount) { 270 poolProps.createdCount--; 278 totalThreadDestroyedCount++; return; 280 } 281 continue; 283 } 284 canCreateBuddy = (waitCount == 0) && 285 (poolProps.createdCount < poolProps.maxThreadCount); 286 if (canCreateBuddy) poolProps.createdCount++; 288 numMessages++; 289 } 290 if (canCreateBuddy) { 291 new ThreadPoolThread(poolProps); 297 } 298 299 try { 305 task.prolog(); 306 task.service(); 307 task.epilog(); 308 } catch (Throwable th) { 309 _logger.log(Level.SEVERE,"iplanet_util.generic_exception",th); 312 } 314 } while (task != null); 315 } 316 } catch (com.sun.enterprise.util.collection.QueueClosedException qcEx) { 317 _logger.log(Level.FINE,"Queue closed. Exitting...."); 320 synchronized (poolProps) { 322 poolProps.createdCount--; 323 } 324 totalThreadDestroyedCount++; return; 326 } catch (InterruptedException inEx) { 327 _logger.log(Level.SEVERE,"iplanet_util.generic_exception",inEx); 330 synchronized (poolProps) { 332 poolProps.createdCount--; 333 } 334 totalThreadDestroyedCount++; return; 336 } 337 } 338 339 } 340 341 342 } 343 | Popular Tags |