com.go.trove.util
Class ThreadPool

java.lang.Object
  |
  +--java.lang.ThreadGroup
        |
        +--com.go.trove.util.ThreadPool

public class ThreadPool
extends java.lang.ThreadGroup

A ThreadPool contains a collection of re-usable threads. There is a slight performance overhead in creating new threads, and so a ThreadPool can improve performance in systems that create short-lived threads. Pooled threads operate on Runnable targets and return back to the pool when the Runnable.run method exits.

Version:
8 , 01/03/12
Author:
Brian S O'Neill

Constructor Summary
ThreadPool(java.lang.String name, int max)
          Create a ThreadPool of daemon threads.
ThreadPool(java.lang.String name, int max, boolean daemon)
          Create a ThreadPool.
ThreadPool(java.lang.ThreadGroup parent, java.lang.String name, int max)
          Create a ThreadPool of daemon threads.
ThreadPool(java.lang.ThreadGroup parent, java.lang.String name, int max, boolean daemon)
          Create a ThreadPool.
 
Method Summary
 void addThreadPoolListener(ThreadPoolListener listener)
           
 void close()
          Will close down all the threads in the pool as they become available.
 void close(long timeout)
          Will close down all the threads in the pool as they become available.
 java.lang.Thread[] getAllThreads()
           
 int getAvailableCount()
           
 long getIdleTimeout()
          Returns the idle timeout (in milliseconds) for threads to exit.
 int getMaximumAllowed()
           
 int getPooledCount()
           
 int getPriority()
          Returns the initial priority given to each thread in the pool.
 int getThreadCount()
           
 long getTimeout()
          Returns the timeout (in milliseconds) for getting threads from the pool.
 boolean isClosed()
           
 void removeThreadPoolListener(ThreadPoolListener listener)
           
 void setIdleTimeout(long timeout)
          Sets the timeout (in milliseconds) for idle threads to exit.
 void setPriority(int priority)
          Sets the priority given to each thread in the pool.
 void setTimeout(long timeout)
          Sets the timeout (in milliseconds) for getting threads from the pool or for closing the pool.
 java.lang.Thread start(java.lang.Runnable target)
          Waits for a Thread to become available and starts a Runnable in it.
 java.lang.Thread start(java.lang.Runnable target, long timeout)
          Waits for a Thread to become available and starts a Runnable in it.
 java.lang.Thread start(java.lang.Runnable target, long timeout, java.lang.String name)
          Waits for a Thread to become available and starts a Runnable in it.
 java.lang.Thread start(java.lang.Runnable target, java.lang.String name)
          Waits for a Thread to become available and starts a Runnable in it.
 
Methods inherited from class java.lang.ThreadGroup
activeCount, activeGroupCount, allowThreadSuspension, checkAccess, destroy, enumerate, enumerate, enumerate, enumerate, getMaxPriority, getName, getParent, interrupt, isDaemon, isDestroyed, list, parentOf, resume, setDaemon, setMaxPriority, stop, suspend, toString, uncaughtException
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ThreadPool

public ThreadPool(java.lang.String name,
                  int max)
           throws java.lang.IllegalArgumentException
Create a ThreadPool of daemon threads.
Parameters:
name - Name of ThreadPool
max - The maximum allowed number of threads
Throws:
java.lang.IllegalArgumentException -  

ThreadPool

public ThreadPool(java.lang.ThreadGroup parent,
                  java.lang.String name,
                  int max)
           throws java.lang.IllegalArgumentException
Create a ThreadPool of daemon threads.
Parameters:
parent - Parent ThreadGroup
name - Name of ThreadPool
max - The maximum allowed number of threads
Throws:
java.lang.IllegalArgumentException -  

ThreadPool

public ThreadPool(java.lang.String name,
                  int max,
                  boolean daemon)
           throws java.lang.IllegalArgumentException
Create a ThreadPool.
Parameters:
name - Name of ThreadPool
max - The maximum allowed number of threads
daemon - Set to true to create ThreadPool of daemon threads
Throws:
java.lang.IllegalArgumentException -  

ThreadPool

public ThreadPool(java.lang.ThreadGroup parent,
                  java.lang.String name,
                  int max,
                  boolean daemon)
           throws java.lang.IllegalArgumentException
Create a ThreadPool.
Parameters:
parent - Parent ThreadGroup
name - Name of ThreadPool
max - The maximum allowed number of threads
daemon - Set to true to create ThreadPool of daemon threads
Throws:
java.lang.IllegalArgumentException -  
Method Detail

setTimeout

public void setTimeout(long timeout)
Sets the timeout (in milliseconds) for getting threads from the pool or for closing the pool. A negative value specifies an infinite timeout. Calling the start method that accepts a timeout value will override this setting.

getTimeout

public long getTimeout()
Returns the timeout (in milliseconds) for getting threads from the pool. The default value is negative, which indicates an infinite wait.

setIdleTimeout

public void setIdleTimeout(long timeout)
Sets the timeout (in milliseconds) for idle threads to exit. A negative value specifies that an idle thread never exits.

getIdleTimeout

public long getIdleTimeout()
Returns the idle timeout (in milliseconds) for threads to exit. The default value is negative, which indicates that idle threads never exit.

addThreadPoolListener

public void addThreadPoolListener(ThreadPoolListener listener)

removeThreadPoolListener

public void removeThreadPoolListener(ThreadPoolListener listener)

getPriority

public int getPriority()
Returns the initial priority given to each thread in the pool. The default value is that of the thread that created the ThreadPool.

setPriority

public void setPriority(int priority)
                 throws java.lang.IllegalArgumentException
Sets the priority given to each thread in the pool.
Throws:
java.lang.IllegalArgumentException - if priority is out of range

getMaximumAllowed

public int getMaximumAllowed()
Returns:
The maximum allowed number of threads.

getAvailableCount

public int getAvailableCount()
Returns:
The number of currently available threads in the pool.

getPooledCount

public int getPooledCount()
Returns:
The total number of threads in the pool that are either available or in use.

getThreadCount

public int getThreadCount()
Returns:
The total number of threads in the ThreadGroup.

getAllThreads

public java.lang.Thread[] getAllThreads()
Returns:
Each thread that is active in the entire ThreadGroup.

start

public java.lang.Thread start(java.lang.Runnable target)
                       throws NoThreadException,
                              java.lang.InterruptedException
Waits for a Thread to become available and starts a Runnable in it. If there are no available threads and the number of active threads is less than the maximum allowed, then a newly created thread is returned.
Parameters:
target - The Runnable instance that gets started by the returned thread.
Returns:
A Thread that has been started on the given Runnable.
Throws:
NoThreadException - If no thread could be obtained.
java.lang.InterruptedException - If interrupted while waiting for a thread to become available.

start

public java.lang.Thread start(java.lang.Runnable target,
                              long timeout)
                       throws NoThreadException,
                              java.lang.InterruptedException
Waits for a Thread to become available and starts a Runnable in it. If there are no available threads and the number of active threads is less than the maximum allowed, then a newly created thread is returned.
Parameters:
target - The Runnable instance that gets started by the returned thread.
timeout - Milliseconds to wait for a thread to become available. If zero, don't wait at all. If negative, wait forever.
Returns:
A Thread that has been started on the given Runnable.
Throws:
NoThreadException - If no thread could be obtained.
java.lang.InterruptedException - If interrupted while waiting for a thread to become available.

start

public java.lang.Thread start(java.lang.Runnable target,
                              java.lang.String name)
                       throws NoThreadException,
                              java.lang.InterruptedException
Waits for a Thread to become available and starts a Runnable in it. If there are no available threads and the number of active threads is less than the maximum allowed, then a newly created thread is returned.
Parameters:
target - The Runnable instance that gets started by the returned thread.
name - The name to give the thread.
Returns:
A Thread that has been started on the given Runnable.
Throws:
NoThreadException - If no thread could be obtained.
java.lang.InterruptedException - If interrupted while waiting for a thread to become available.

start

public java.lang.Thread start(java.lang.Runnable target,
                              long timeout,
                              java.lang.String name)
                       throws NoThreadException,
                              java.lang.InterruptedException
Waits for a Thread to become available and starts a Runnable in it. If there are no available threads and the number of active threads is less than the maximum allowed, then a newly created thread is returned.
Parameters:
target - The Runnable instance that gets started by the returned thread.
timeout - Milliseconds to wait for a thread to become
name - The name to give the thread. available. If zero, don't wait at all. If negative, wait forever.
Returns:
A Thread that has been started on the given Runnable.
Throws:
NoThreadException - If no thread could be obtained.
java.lang.InterruptedException - If interrupted while waiting for a thread to become available.

isClosed

public boolean isClosed()

close

public void close()
           throws java.lang.InterruptedException
Will close down all the threads in the pool as they become available. This method may block forever if any threads are never returned to the thread pool.

close

public void close(long timeout)
           throws java.lang.InterruptedException
Will close down all the threads in the pool as they become available. If all the threads cannot become available within the specified timeout, any active threads not yet returned to the thread pool are interrupted.
Parameters:
timeout - Milliseconds to wait before unavailable threads are interrupted. If zero, don't wait at all. If negative, wait forever.