1 22 23 package org.jboss.aspects.asynchronous.concurrent; 24 25 import EDU.oswego.cs.dl.util.concurrent.PooledExecutor; 26 import EDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser; 27 import org.jboss.aspects.asynchronous.AsynchronousConstants; 28 import org.jboss.aspects.asynchronous.AsynchronousParameters; 29 import org.jboss.aspects.asynchronous.AsynchronousTask; 30 import org.jboss.aspects.asynchronous.AsynchronousUserTask; 31 import org.jboss.aspects.asynchronous.ThreadManager; 32 import org.jboss.aspects.asynchronous.ThreadManagerRequest; 33 import org.jboss.aspects.asynchronous.ThreadManagerResponse; 34 import org.jboss.aspects.asynchronous.common.AsynchronousEmptyTask; 35 import org.jboss.aspects.asynchronous.common.ThreadManagerResponseImpl; 36 37 38 42 43 public class ThreadManagerImpl 44 45 extends ThreadFactoryUser 46 47 implements AsynchronousConstants, ThreadManager 48 { 49 50 protected PooledExecutor _pooledExecutor = null; 51 52 protected boolean waitWhenPoolSizeIsFull = true; 53 54 protected boolean isPooling = true; 55 56 59 60 public ThreadManagerImpl() 61 { 62 63 _pooledExecutor = new PooledExecutor(); 64 65 setWaitWhenPoolSizeIsFull(false); 66 67 } 68 69 74 75 public ThreadManagerImpl(int maximumPoolSize) 76 { 77 78 _pooledExecutor = new PooledExecutor(maximumPoolSize); 79 80 setWaitWhenPoolSizeIsFull(false); 81 82 } 83 84 95 96 public void setMaximumPoolSize(int maximumPoolSize) 97 { 98 99 _pooledExecutor.setMaximumPoolSize(maximumPoolSize); 100 101 } 102 103 118 119 public void setWaitWhenPoolSizeIsFull(boolean value) 120 { 121 122 if (value) 123 124 _pooledExecutor.waitWhenBlocked(); 125 126 else 127 128 _pooledExecutor.abortWhenBlocked(); 129 130 waitWhenPoolSizeIsFull = value; 131 132 } 133 134 137 138 public boolean getWaitWhenPoolSizeIsFull() 139 { 140 141 return waitWhenPoolSizeIsFull; 142 143 } 144 145 148 149 public int getMaximumPoolSize() 150 { 151 152 return _pooledExecutor.getMaximumPoolSize(); 153 154 } 155 156 167 168 public void setMinimumPoolSize(int minimumPoolSize) 169 { 170 171 _pooledExecutor.setMinimumPoolSize(minimumPoolSize); 172 173 } 174 175 184 185 public int getMinimumPoolSize() 186 { 187 188 return _pooledExecutor.getMinimumPoolSize(); 189 190 } 191 192 199 200 public void setKeepAliveTime(long time) 201 { 202 203 _pooledExecutor.setKeepAliveTime(time); 204 205 } 206 207 214 215 public long getKeepAliveTime() 216 { 217 218 return _pooledExecutor.getKeepAliveTime(); 219 220 } 221 222 229 230 public long getPoolSize() 231 { 232 233 return _pooledExecutor.getPoolSize(); 234 235 } 236 237 244 245 public ThreadManagerResponse waitForResponse(AsynchronousTask input) 246 { 247 248 AsynchronousTask[] tTask = {input}; 249 250 return waitForResponses(tTask)[0]; 251 252 } 253 254 259 260 public ThreadManagerResponse[] waitForResponses(AsynchronousTask[] inputImpl) 261 { 262 263 if (inputImpl == null) 264 { 265 266 System.err.println("PPMImpl:waitForResponses NULL PARAMETER"); 267 268 return null; 269 270 } 271 272 ThreadManagerResponse[] response = 273 274 new ThreadManagerResponseImpl[inputImpl.length]; 275 276 for (int i = 0; i < inputImpl.length; i++) 277 { 278 279 AsynchronousTask fr = inputImpl[i]; 280 281 response[i] = fr.getResponse(); 282 283 } 284 285 return response; 286 287 } 288 289 public AsynchronousTask process(ThreadManagerRequest ppmRequest) 290 { 291 292 return process(ppmRequest.getId(), 293 294 ppmRequest.getTaskClassImpl(), 295 296 ppmRequest.getInputParameters(), 297 298 ppmRequest.getTimeout()); 299 300 } 301 302 313 314 private AsynchronousTask process(String id, 315 316 AsynchronousUserTask taskImpl, 317 318 AsynchronousParameters inputParametersImpl, 319 320 long timeout) 321 { 322 323 try 324 { 325 326 327 if (this.getPoolSize() + 1 > this.getMaximumPoolSize()) 328 329 System.err.println("process: The maximum pool size defined at " 330 331 + this.getMaximumPoolSize() 332 333 + " is reached before processing task[" 334 335 + id 336 337 + "] !"); 338 339 org.jboss.aspects.asynchronous.concurrent.AsynchronousTask ft = 340 341 new AsynchronousTaskImpl(id, 342 343 taskImpl, 344 345 inputParametersImpl, 346 347 timeout); 348 349 Runnable cmd = ft.add(); 350 351 if (isPooling()) 352 353 _pooledExecutor.execute(cmd); 354 355 else 356 { 357 358 Thread thread = getThreadFactory().newThread(cmd); 359 360 thread.start(); 361 362 } 363 364 Thread.yield(); 365 366 Thread.sleep(0); 367 368 Thread.yield(); 369 370 return ft; 371 372 } 373 catch (Exception e) 374 { 375 376 377 return new AsynchronousEmptyTask(id, 378 379 AsynchronousConstants.CAN_NOT_PROCESS, 380 381 e, 382 383 e.getMessage(), 384 385 System.currentTimeMillis()); 386 387 } 388 389 } 390 391 public boolean isPooling() 392 { 393 394 return isPooling; 395 396 } 397 398 public void setPooling(boolean isPooling) 399 { 400 401 this.isPooling = isPooling; 402 403 } 404 405 } 406 407 | Popular Tags |