com.resilientedge.util.workerPool
Class WorkerPool

java.lang.Object
  |
  +--com.resilientedge.util.workerPool.WorkerPool
All Implemented Interfaces:
java.io.Serializable

public class WorkerPool
extends java.lang.Object
implements java.io.Serializable

Provides a pool of daemon threads (workers). The minimum and maximum pool sizes are set during initialization. If a request is made and there are no available workers, the requestor is put into stasis until a worker becomes available. The worker pool is thread safe, and may be shared among multiple threads. A FIFO queueing process is used for multiple requestors waiting on a worker to become available from the pool.

An optional listener may be specified for the pool. The listener, an instance of WorkerPoolListener, is notified when events are generated by the pool. These events, instances of WorkerPoolEvent, include a call-back when a unit of work has completed on one of the workers in the pool. The listener may defined during worker pool initialization or changed at anytime during execution. Only one listener may registered.

Example Usage: create a pool with 2 min workers, 5 max workers, the given worker class, and no listener WorkerPool workerPool = new WorkerPool(); workerPool.init("A Worker Pool",2,5,QueueOutWorker.class,null);

Author:
jgardner
See Also:
Serialized Form

Constructor Summary
WorkerPool()
          Default constructor.
 
Method Summary
protected  void addToQueue(int cnt)
          Add the specified number of threads to the queue.
protected  boolean createEvent(int id)
          If there is a listener, create an event and notify the listener of the event.
protected  boolean createEvent(int id, java.lang.Object obj)
          If there is a listener, create an event and notify the listener of the event.
protected  com.resilientedge.util.workerPool.WorkerPool.PoolThread createWorker()
          Create a new thread and add it to the thread-pool.
 void dispose()
          The Worker pool is cleared.
 void doWork(java.lang.Object data)
          Request a work to be done on a thread from the worker pool.
 WorkerPoolListener getListener()
           
 int getMaxSize()
           
 int getMinSize()
           
 WaitQStats getWaitStats()
           
protected  java.lang.Class getWorkClass()
           
protected  com.resilientedge.util.workerPool.WorkerPool.PoolThread getWorker()
          Get a thread from the pool.
protected  java.lang.String getWorkerId()
          The thread identifier prefix is based on the pool name.
 java.lang.String getWorkerPoolName()
           
 void init(java.lang.String nm, int min, int max, java.lang.Class cls, WorkerPoolListener lstnr)
          Initialize the pool, this should only be called once.
 void setListener(WorkerPoolListener lstnr)
           
protected  void setMaxSize(int sz)
          If the requested maximum size is less than the minimum size, the minimum size is used.
protected  void setMinSize(int sz)
           
protected  void setWorkClass(java.lang.Class cls)
           
protected  void setWorkerPoolName(java.lang.String nm)
          Save the name of the pool, and create the prefix for the thread identifiers, which is based on the pool name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WorkerPool

public WorkerPool()
Default constructor. Initialization must be requested before the pool can be used.
Method Detail

setListener

public void setListener(WorkerPoolListener lstnr)
Parameters:
lstnr - the thread pool listener or null if no listener

getMinSize

public int getMinSize()
Returns:
the minimum pool size

getMaxSize

public int getMaxSize()
Returns:
the maximum pool size

getWaitStats

public WaitQStats getWaitStats()
Returns:
the wait queue stats

getWorkerPoolName

public java.lang.String getWorkerPoolName()
Returns:
the name of the Worker pool

getListener

public WorkerPoolListener getListener()
Returns:
the thread pool listener or null if no listener

init

public void init(java.lang.String nm,
                 int min,
                 int max,
                 java.lang.Class cls,
                 WorkerPoolListener lstnr)
Initialize the pool, this should only be called once. The minimum number of pool entries is created and initialized.
Parameters:
nm - the name of the pool, this name is used in each worker that is created for the pool and in the wait queue statistics associated associated with the pool
min - the minimum number of pool entries
max - the maximum number of pool entries, if less than minimum, minimum is used
cls - the class that implements the Worker interface
lstnr - a listener that is to receive pool events, may be null

dispose

public void dispose()
The Worker pool is cleared. Each worker in the pool is terminated, if the worker is active the requestor will wait until the worker in the pool has completed processing. If a pool worker is inactive, the worker is requested to terminate. The requestor waits for each worker to complete termination before advancing to the next worker in the pool.

If subsequent requests are made of the worker pool, new workers will be created, up to the maximum specified during initialization.


doWork

public void doWork(java.lang.Object data)
Request a work to be done on a thread from the worker pool.
Parameters:
data - Parameter Description

setMinSize

protected void setMinSize(int sz)
Parameters:
sz - the minimum pool size

setMaxSize

protected void setMaxSize(int sz)
If the requested maximum size is less than the minimum size, the minimum size is used.
Parameters:
sz - the maximum pool size

setWorkClass

protected void setWorkClass(java.lang.Class cls)
Parameters:
cls - the class that implements the Work interface

setWorkerPoolName

protected void setWorkerPoolName(java.lang.String nm)
Save the name of the pool, and create the prefix for the thread identifiers, which is based on the pool name.
Parameters:
nm - the name of the thread pool

getWorker

protected com.resilientedge.util.workerPool.WorkerPool.PoolThread getWorker()
Get a thread from the pool. If a thread is not available, and cannot be created, the requestor will be queued until a thread is freed and can accept a new request.
Returns:
a thread from the pool

getWorkClass

protected java.lang.Class getWorkClass()
Returns:
the class that implements the Work interface

getWorkerId

protected java.lang.String getWorkerId()
The thread identifier prefix is based on the pool name.
Returns:
the prefix for the thread identifiers

addToQueue

protected void addToQueue(int cnt)
Add the specified number of threads to the queue.
Parameters:
cnt - the number of entries to be created in the pool

createWorker

protected com.resilientedge.util.workerPool.WorkerPool.PoolThread createWorker()
Create a new thread and add it to the thread-pool. If the thread cannot be created, a runtime exception is thrown. If there is a listener, the listener is notified of the addition after the thread is added to the pool.
Returns:
a new thread

createEvent

protected boolean createEvent(int id)
If there is a listener, create an event and notify the listener of the event.
Parameters:
id - the event identifier
Returns:
Returned Value Description

createEvent

protected boolean createEvent(int id,
                              java.lang.Object obj)
If there is a listener, create an event and notify the listener of the event.
Parameters:
id - the event identifier
obj - an object that is passed to the listener, the type of object is event dependent
Returns:
true if a listener was notified of the event


Copyright © 2001 Resilient Edge, Inc. All Rights Reserved.