FileDocCategorySizeDatePackage
ThreadManagerImpl.javaAPI DocJBoss 4.2.19132Fri Jul 13 21:02:30 BST 2007org.jboss.aspects.asynchronous.concurrent

ThreadManagerImpl

public class ThreadManagerImpl extends EDU.oswego.cs.dl.util.concurrent.ThreadFactoryUser implements org.jboss.aspects.asynchronous.AsynchronousConstants, org.jboss.aspects.asynchronous.ThreadManager
author
{Claude Hussenet Independent Consultant}.
version
$Revision: 57186 $

Fields Summary
protected EDU.oswego.cs.dl.util.concurrent.PooledExecutor
_pooledExecutor
protected boolean
waitWhenPoolSizeIsFull
protected boolean
isPooling
Constructors Summary
public ThreadManagerImpl()
Create a new pool with all default settings


               

    
   

      _pooledExecutor = new PooledExecutor();

      setWaitWhenPoolSizeIsFull(false);

   
public ThreadManagerImpl(int maximumPoolSize)
Create a new pool with all default settings except

for maximum pool size.


      _pooledExecutor = new PooledExecutor(maximumPoolSize);

      setWaitWhenPoolSizeIsFull(false);

   
Methods Summary
public longgetKeepAliveTime()
Return the number of milliseconds to keep threads alive waiting

for new commands. A negative value means to wait forever. A zero

value means not to wait at all.


      return _pooledExecutor.getKeepAliveTime();

   
public intgetMaximumPoolSize()
Return the maximum number of threads to simultaneously execute


      return _pooledExecutor.getMaximumPoolSize();

   
public intgetMinimumPoolSize()
Return the minimum number of threads to simultaneously execute.

(Default value is 1). If fewer than the mininum number are

running upon reception of a new request, a new thread is started

to handle this request.


      return _pooledExecutor.getMinimumPoolSize();

   
public longgetPoolSize()
Return the current number of active threads in the pool. This

number is just a snaphot, and may change immediately upon

returning


      return _pooledExecutor.getPoolSize();

   
public booleangetWaitWhenPoolSizeIsFull()
return the policy when the pool is full


      return waitWhenPoolSizeIsFull;

   
public booleanisPooling()


      return isPooling;

   
public org.jboss.aspects.asynchronous.AsynchronousTaskprocess(org.jboss.aspects.asynchronous.ThreadManagerRequest ppmRequest)


      return process(ppmRequest.getId(),

      ppmRequest.getTaskClassImpl(),

      ppmRequest.getInputParameters(),

      ppmRequest.getTimeout());

   
private org.jboss.aspects.asynchronous.AsynchronousTaskprocess(java.lang.String id, org.jboss.aspects.asynchronous.AsynchronousUserTask taskImpl, org.jboss.aspects.asynchronous.AsynchronousParameters inputParametersImpl, long timeout)
Create, start and return a new asynchronous task from :

taskImpl class instance defining the task to process

inputParametersImpl class instance defining the input parameters

timeout defined the given time limit to process the task


      try
      {


         if (this.getPoolSize() + 1 > this.getMaximumPoolSize())

            System.err.println("process: The maximum pool size defined at "

            + this.getMaximumPoolSize()

            + " is reached before processing task["

            + id

            + "] !");

         org.jboss.aspects.asynchronous.concurrent.AsynchronousTask ft =

         new AsynchronousTaskImpl(id,

         taskImpl,

         inputParametersImpl,

         timeout);

         Runnable cmd = ft.add();

         if (isPooling())

            _pooledExecutor.execute(cmd);

         else
         {

            Thread thread = getThreadFactory().newThread(cmd);

            thread.start();

         }

         Thread.yield();

         Thread.sleep(0);

         Thread.yield();

         return ft;

      }
      catch (Exception e)
      {


         return new AsynchronousEmptyTask(id,

         AsynchronousConstants.CAN_NOT_PROCESS,

         e,

         e.getMessage(),

         System.currentTimeMillis());

      }

   
public voidsetKeepAliveTime(long time)
Set the number of milliseconds to keep threads alive waiting for

new commands. A negative value means to wait forever. A zero

value means not to wait at all.


      _pooledExecutor.setKeepAliveTime(time);

   
public voidsetMaximumPoolSize(int maximumPoolSize)
Set the minimum number of threads to use.

throws
IllegalArgumentException if less than zero. (It is not

considered an error to set the minimum to be greater than the

maximum. However, in this case there are no guarantees about

behavior.)


      _pooledExecutor.setMaximumPoolSize(maximumPoolSize);

   
public voidsetMinimumPoolSize(int minimumPoolSize)
Set the minimum number of threads to use.

throws
IllegalArgumentException if less than zero. (It is not

considered an error to set the minimum to be greater than the

maximum. However, in this case there are no guarantees about

behavior.)


      _pooledExecutor.setMinimumPoolSize(minimumPoolSize);

   
public voidsetPooling(boolean isPooling)


      this.isPooling = isPooling;

   
public voidsetWaitWhenPoolSizeIsFull(boolean value)
Set the policy for blocked execution to be to wait until a thread

is available.

OR

Set the policy for blocked execution to be to

throw a RuntimeException.


      if (value)

         _pooledExecutor.waitWhenBlocked();

      else

         _pooledExecutor.abortWhenBlocked();

      waitWhenPoolSizeIsFull = value;

   
public org.jboss.aspects.asynchronous.ThreadManagerResponsewaitForResponse(org.jboss.aspects.asynchronous.AsynchronousTask input)
Return the response from an asynchronous task

The call returns within the timeout defined

in the process method


      AsynchronousTask[] tTask = {input};

      return waitForResponses(tTask)[0];

   
public org.jboss.aspects.asynchronous.ThreadManagerResponse[]waitForResponses(org.jboss.aspects.asynchronous.AsynchronousTask[] inputImpl)
Return an array of responses from an array of asynchronous task

The call returns within the maximum timeout from the array of tasks


      if (inputImpl == null)
      {

         System.err.println("PPMImpl:waitForResponses NULL PARAMETER");

         return null;

      }

      ThreadManagerResponse[] response =

      new ThreadManagerResponseImpl[inputImpl.length];

      for (int i = 0; i < inputImpl.length; i++)
      {

         AsynchronousTask fr = inputImpl[i];

         response[i] = fr.getResponse();

      }

      return response;