ewe.sys
Class ThreadPool

java.lang.Object
  extended byewe.sys.TaskObject
      extended byewe.sys.ThreadPool
All Implemented Interfaces:
Runnable, Task

public class ThreadPool
extends TaskObject

A ThreadPool represents a pool of running Threads that are waiting for tasks to run. You call addTask() to add a Task Object to the pool and an idle Thread is assigned to run the task. If no idle Thread is available a new one is created and used. ThreadPools can greatly improve performance in server applications since it reduces the overhead of creating new Threads. This has a greater effect under a true Java VM since Ewe Threads are very lightweight and really don't take any more system resources than any other object.

You can specify a minimum number of idle Threads and Threads are added to the pool to make up to that minimum.

You can specify a maximum number of Threads and if the maximum is reached all new tasks will have to wait until older ones are finished and their Threads are freed. If all Threads are idle and there are more idle threads than the specified minimum, the Threads will eventually terminate and be discarded until only the minimum number of Threads remain.


Field Summary
 int maxThreads
          The maximum number of allowed threads.
 int minThreads
          The minimum number of allowed threads.
 int waitTime
          How long (in seconds) before an idle thread decides to exit (if there are no tasks to run and if the number of threads is already greater than the minimum).
 
Fields inherited from class ewe.sys.TaskObject
handle, interruptThreadToStop, myThread, napIterations, napTime, shouldStop
 
Constructor Summary
ThreadPool()
          Create a ThreadPool with a minimum of 10 threads and no maximum.
ThreadPool(int min, int max)
          Create a ThreadPool specifying the minimum and maximum number of threads.
 
Method Summary
 void addTask(Runnable toRun)
          Use this to add a task to run.
 void addTask(Runnable toRun, int timeOutInSeconds)
          Use this to add a task to run.
 void close()
          After calling this no more tasks can be added and any spare threads will eventually die.
protected  void doRun()
          Override this method if you want to use an mThread to run your Task.
 void makeUpToMinimum()
          Add threads until the minimum has been achieved.
 
Methods inherited from class ewe.sys.TaskObject
checkAbortFail, checkFailure, doStart, doStop, exec, getHandle, nap, run, setNapTime, show, sleep, start, startTask, stopTask, waitOn, waitOnAny, waitOnSuccess, yield, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, toString
 

Field Detail

minThreads

public int minThreads
The minimum number of allowed threads. By default this is 10.


maxThreads

public int maxThreads
The maximum number of allowed threads. If this is <0 there is no maximum (the default).


waitTime

public int waitTime
How long (in seconds) before an idle thread decides to exit (if there are no tasks to run and if the number of threads is already greater than the minimum). By default it is 5.

Constructor Detail

ThreadPool

public ThreadPool()
Create a ThreadPool with a minimum of 10 threads and no maximum.


ThreadPool

public ThreadPool(int min,
                  int max)
Create a ThreadPool specifying the minimum and maximum number of threads.

Method Detail

makeUpToMinimum

public void makeUpToMinimum()
Add threads until the minimum has been achieved. If you have increased the minimum then you can optionally call this.


doRun

protected void doRun()
Description copied from class: TaskObject
Override this method if you want to use an mThread to run your Task. Otherwise if you are using timers or callbacks, override the doStart() method.

Overrides:
doRun in class TaskObject

addTask

public void addTask(Runnable toRun,
                    int timeOutInSeconds)
             throws IllegalStateException
Use this to add a task to run. This should be a Runnable, including a TaskObject.

Parameters:
toRun - The task to run.
timeOutInSeconds - A maximum length of time for the task to run for. If this is zero or less then the task will have no maximum time.
Throws:
IllegalStateException - if the ThreadPool is closed.

addTask

public void addTask(Runnable toRun)
             throws IllegalStateException
Use this to add a task to run. This should be a Runnable, including a TaskObject.

Parameters:
toRun - The task to run.
Throws:
IllegalStateException - if the ThreadPool is closed.

close

public void close()
After calling this no more tasks can be added and any spare threads will eventually die.