net.larsan.urd.util
Class ThreadPool

java.lang.Object
  |
  +--net.larsan.urd.util.ThreadPool
All Implemented Interfaces:
Executor

public class ThreadPool
extends java.lang.Object
implements Executor

The thread pool is an abstract pool of thread. The subclasses must specify four methods: creation and destruction of single threads and handling before and after execution. This makes it possible to use the pool with subclasses of Thread and a possibility to set thread local variables such as class loaders before execution.

Users of the thread pool simply pass a Runnable into the execute method and form that point on the pool controls the execution of the code.

This thread pool can be started with a minimum and maximum of threads. The minimum threads will be created on pool startup. The maximum number is optional and specifies the absolute maximum number of threads possible. Further tweaking is possible with the setExceedingThreadTTL method which sets the timeout for unused threads when the pool exceeds the minimum pool size, and the setExcecuteQueueTimeout which allows the pool to wait for free threads when a request to execute is made but the max number of threads is already reached.

Version:
Alpha-0.1 / 2002-10-06 21:37
Author:
Lars J. Nilsson

Constructor Summary
ThreadPool(java.lang.String name)
          Contruct thread pool.
ThreadPool(java.lang.String name, int minimum)
          Contruct thread pool given a minimum number of threads.
ThreadPool(java.lang.String name, int minimum, int maximum)
          Contruct thread pool given a minimum and a maximum number of threads.
 
Method Summary
protected  void afterExecution(java.lang.Runnable target)
          This method is called just after a target is executed by the thread pool by the thread that will execute the target.
protected  void beforeExecution(java.lang.Runnable target)
          This method is called just before a target is executed by the thread pool by the thread that will execute the target.
 void close()
          Close the pool.
protected  java.lang.Thread createThread(java.lang.Runnable handle)
          Create a new thread.
protected  void destroyThread(java.lang.Thread thread)
          Destroy a thread.
 void execute(java.lang.Runnable target)
          Execute a runnable.
 long getExcecuteQueueTimeout()
          Get the time an execution request may wait if there is no threads available before throwing an ExecutionDeniedException.
 int getMaximumSize()
          Get the maximum size of this thread pool.
 int getMinimumSize()
          Get the minimum size of this thread pool.
 long getThreadTTL()
          Get the time in milliseconds a thread will live if not used and the pool size is above it's minimum size.
 void setExcecuteQueueTimeout(long millis)
          Get the time an execution request may wait if there is no threads available before throwing an ExecutionDeniedException.
 void setMaximumSize(int maximum)
          Set the maximum size of this thread pool.
 void setThreadTTL(long millis)
          Set the time in milliseconds a thread will live if not used and the pool size is above it's minimum size.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool(java.lang.String name,
                  int minimum,
                  int maximum)
Contruct thread pool given a minimum and a maximum number of threads. The minimum size must not be below zero and the maximum must not be below minimum except for -1 which signals that there is no maximum size.

The default thread TTL is 30 minutes.

Parameters:
name - The pool name, used for all new threads
minimum - The minimum number of threads in the pool, must be above zero
maximum - The maximum number of threads in the pool, -1 to disable

ThreadPool

public ThreadPool(java.lang.String name,
                  int minimum)
Contruct thread pool given a minimum number of threads. The minimum size must not be below zero and there willl be no maximum size.

The default thread TTL is 30 minutes.

Parameters:
name - The pool name, used for new threads
minimum - The minimum number of threads in the pool, must be above zero

ThreadPool

public ThreadPool(java.lang.String name)
Contruct thread pool. The default thread TTL is 30 minutes. here will be no minimum number of thread in the pool and no maximum either.

Parameters:
name - The pool name, used for new threads
Method Detail

execute

public void execute(java.lang.Runnable target)
             throws java.lang.IllegalStateException
Execute a runnable. This method will take a thread from the pool and call the runnables run method. Should the pool be full, be closed or get an internal error a IllegalStateException will be thrown.

Specified by:
execute in interface Executor
Parameters:
target - Runnable to execute by a thread from the pool
Throws:
java.lang.IllegalStateException - If the pool does not accept the target

close

public void close()
Close the pool. This will notify all threads of their comming demise but will not wait for their execution to finish.


getExcecuteQueueTimeout

public long getExcecuteQueueTimeout()
Get the time an execution request may wait if there is no threads available before throwing an ExecutionDeniedException. This returns -1 if the wait is disabled.

Returns:
A millisecond value for execution request wait, or -1 if disabled

setExcecuteQueueTimeout

public void setExcecuteQueueTimeout(long millis)
Get the time an execution request may wait if there is no threads available before throwing an ExecutionDeniedException. The wait can be disabled by setting the value to -1.

Parameters:
millis - A millisecond value representing the wait, -1 for disabling

getThreadTTL

public long getThreadTTL()
Get the time in milliseconds a thread will live if not used and the pool size is above it's minimum size. This return -1 if there is no such timeout.

Returns:
A millisecod value for thread timeouts, or -1 if disabled

setThreadTTL

public void setThreadTTL(long millis)
Set the time in milliseconds a thread will live if not used and the pool size is above it's minimum size. This value can be set to -1 if there should be no such timeout.


getMinimumSize

public int getMinimumSize()
Get the minimum size of this thread pool. This value will always be above zero and is immutable.

Returns:
The minimum size of this pool, always a possitive integer

getMaximumSize

public int getMaximumSize()
Get the maximum size of this thread pool. This value will always be bigger than the minimum size and can be changed dinamicly . This returns -1 if there is no roof on this pool.

Returns:
The maximum size of this pool, always bigger than the minimum size or -1 if disabled

setMaximumSize

public void setMaximumSize(int maximum)
                    throws java.lang.IllegalArgumentException
Set the maximum size of this thread pool. A IllegalArgumentException will be thrown if the value is lower than the minimum size of the pool or negative, except for -1 wich disables the max size.

java.lang.IllegalArgumentException

createThread

protected java.lang.Thread createThread(java.lang.Runnable handle)
Create a new thread. This thread should be created using the runnable parameter which is a handle to control the thread.

Parameters:
handle - Runnable thread handle to use in creation

destroyThread

protected void destroyThread(java.lang.Thread thread)
Destroy a thread. This thread is no longer going to be used be the pool. This gives the subclass a possibility to clean up any additional resources tied to the thread. This implementation is empty.

Parameters:
thread - The thread that leaves the pool, never null

beforeExecution

protected void beforeExecution(java.lang.Runnable target)
This method is called just before a target is executed by the thread pool by the thread that will execute the target. This gives subclasses a chance to setup thread locals and other resources. The default implementation does not do anything.

Parameters:
target - Runnable just to be executed

afterExecution

protected void afterExecution(java.lang.Runnable target)
This method is called just after a target is executed by the thread pool by the thread that will execute the target. This gives subclasses a chance to cleanup thread locals and other resources. The default implementation does not do anything.

Parameters:
target - Runnable just executed