foundry-0.9.1

foundry.threads
Class ThreadPool

java.lang.Object
  |
  +--java.lang.ThreadGroup
        |
        +--foundry.threads.ThreadPool

public class ThreadPool
extends ThreadGroup

A ThreadGroup that pools ThreadPool.PooledThreads. Substantial portions of this class include software developed by GO.com (http://opensource.go.com/); those portions are governed by the Tea Software License, Version 1.0.

Version:
$Revision: 1.4 $
Author:
Laird Nelson, Brian S O'Neill

Inner Class Summary
private  class ThreadPool.PooledThread
           
 
Field Summary
private  int active
          The number of active ThreadPool.PooledThreads in this ThreadPool.
private  long idleTimeout
          The number of milliseconds that ThreadPool.PooledThreads in this ThreadPool should wait before aging off.
private  boolean isClosed
          A flag indicating whether this ThreadPool is closed.
private  boolean isDaemon
          A flag indicating whether new ThreadPool.PooledThreads in this ThreadPool should be created as daemon ThreadPool.PooledThreads.
private  Collection listeners
          A Collection of ThreadPoolListeners that are alerted when ThreadPool.PooledThreads become available and leave this ThreadPool.
private  int max
          The maximum number of ThreadPool.PooledThreads allowed.
private  LinkedList pool
          The pool itself; a LinkedList of ThreadPool.PooledThreads that are idling and ready to perform a job.
private  int priority
          The priority to be given to new ThreadPool.PooledThreads in this ThreadPool.
private static int threadID
          The number that will be used in new ThreadPool.PooledThread names.
private  long timeout
          The number of milliseconds to wait for getting ThreadPool.PooledThreads out of this ThreadPool.
 
Fields inherited from class java.lang.ThreadGroup
daemon, destroyed, groups, maxPriority, name, ngroups, nthreads, parent, threads, vmAllowSuspension
 
Constructor Summary
ThreadPool(String name, int max)
          Creates a ThreadPool of daemon ThreadPool.PooledThreads.
ThreadPool(String name, int max, boolean isDaemon)
          Creates a ThreadPool.
ThreadPool(ThreadGroup parent, String name, int max)
          Creates a ThreadPool of daemon ThreadPool.PooledThreads.
ThreadPool(ThreadGroup parent, String name, int max, boolean isDaemon)
          Creates a ThreadPool.
 
Method Summary
 void addThreadPoolListener(ThreadPoolListener listener)
           
 void close()
          Closes down all the ThreadPool.PooledThreads in this ThreadPool as they become available.
 void close(long timeout)
          Closes down all the ThreadPool.PooledThreads in this ThreadPool as they become available.
private  void ensureOpen()
           
 Thread[] getAllThreads()
           
 int getAvailableCount()
           
 long getIdleTimeout()
          Returns the idle timeout (in milliseconds) for ThreadPool.PooledThreads to exit.
 int getMaximumAllowed()
           
 int getPooledCount()
           
 int getPriority()
          Returns the initial priority given to each thread in this ThreadPool.
 int getThreadCount()
           
 long getTimeout()
          Returns the timeout (in milliseconds) for getting ThreadPool.PooledThreads from this ThreadPool.
private  void init(int max, boolean isDaemon)
          Initializes this ThreadPool so that it will permit only the supplied number of ThreadPool.PooledThreads to exist.
 boolean isClosed()
           
 void removeThreadPoolListener(ThreadPoolListener listener)
           
 void setIdleTimeout(long timeout)
          Sets the timeout (in milliseconds) for idle ThreadPool.PooledThreads to exit.
 void setPriority(int priority)
          Sets the priority given to each thread in this ThreadPool.
 void setTimeout(long timeout)
          Sets the timeout (in milliseconds) for getting ThreadPool.PooledThreads from this ThreadPool or for closing this ThreadPool.
private  Thread[] sort(Thread[] threads)
           
 Thread start(Runnable target)
          Waits for a ThreadPool.PooledThread to become available and starts a Runnable in it.
 Thread start(Runnable target, long timeout)
          Waits for a ThreadPool.PooledThread to become available and starts a Runnable in it.
 Thread start(Runnable target, long timeout, String name)
          Waits for a ThreadPool.PooledThread to become available and starts a Runnable in it.
 Thread start(Runnable target, String name)
          Waits for a ThreadPool.PooledThread to become available and starts a Runnable in it.
private  Thread start0(Runnable target, long timeout, String name)
           
private  ThreadPool.PooledThread startThread(Runnable target, String name)
           
(package private)  void threadAvailable(ThreadPool.PooledThread thread)
           
(package private)  void threadExiting(ThreadPool.PooledThread thread)
           
 
Methods inherited from class java.lang.ThreadGroup
activeCount, activeGroupCount, add, add, allowThreadSuspension, checkAccess, destroy, enumerate, enumerate, enumerate, enumerate, enumerate, enumerate, getMaxPriority, getName, getParent, interrupt, isDaemon, isDestroyed, list, list, parentOf, remove, remove, resume, setDaemon, setMaxPriority, stop, stopOrSuspend, suspend, toString, uncaughtException
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, wait, wait, wait
 

Field Detail

threadID

private static int threadID
The number that will be used in new ThreadPool.PooledThread names.

timeout

private long timeout
The number of milliseconds to wait for getting ThreadPool.PooledThreads out of this ThreadPool. Any negative value is taken to indicate infinity.

idleTimeout

private long idleTimeout
The number of milliseconds that ThreadPool.PooledThreads in this ThreadPool should wait before aging off. Any negative value is taken to indicate infinity.

listeners

private final Collection listeners
A Collection of ThreadPoolListeners that are alerted when ThreadPool.PooledThreads become available and leave this ThreadPool.

pool

private LinkedList pool
The pool itself; a LinkedList of ThreadPool.PooledThreads that are idling and ready to perform a job. Although this field is a LinkedList, it is used for its stack-like features.

max

private int max
The maximum number of ThreadPool.PooledThreads allowed. At no point should the number of ThreadPool.PooledThreads in the pool exceed this number.

active

private int active
The number of active ThreadPool.PooledThreads in this ThreadPool. A ThreadPool.PooledThread is active for this purpose if it is either pooled and idling or running a job.

isDaemon

private boolean isDaemon
A flag indicating whether new ThreadPool.PooledThreads in this ThreadPool should be created as daemon ThreadPool.PooledThreads.

priority

private int priority
The priority to be given to new ThreadPool.PooledThreads in this ThreadPool.

isClosed

private boolean isClosed
A flag indicating whether this ThreadPool is closed.
Constructor Detail

ThreadPool

public ThreadPool(String name,
                  int max)
           throws IllegalArgumentException
Creates a ThreadPool of daemon ThreadPool.PooledThreads.
Parameters:
name - the name of the new ThreadPool
max - the maximum allowed number of ThreadPool.PooledThreads
Throws:
IllegalArgumentException - if max < 0

ThreadPool

public ThreadPool(ThreadGroup parent,
                  String name,
                  int max)
           throws IllegalArgumentException
Creates a ThreadPool of daemon ThreadPool.PooledThreads.
Parameters:
parent - the parent ThreadGroup
name - the name of the new ThreadPool
max - the maximum allowed number of ThreadPool.PooledThreads
Throws:
IllegalArgumentException - if max < 0

ThreadPool

public ThreadPool(String name,
                  int max,
                  boolean isDaemon)
           throws IllegalArgumentException
Creates a ThreadPool.
Parameters:
name - the name of the new ThreadPool
max - the maximum allowed number of ThreadPool.PooledThreads
isDaemon - if true, all ThreadPool.PooledThreads in this ThreadPool will be daemon ThreadPool.PooledThreads.
Throws:
IllegalArgumentException - if max < 0

ThreadPool

public ThreadPool(ThreadGroup parent,
                  String name,
                  int max,
                  boolean isDaemon)
           throws IllegalArgumentException
Creates a ThreadPool.
Parameters:
parent - the parent ThreadGroup
name - the name of the new ThreadPool
max - the maximum allowed number of ThreadPool.PooledThreads
isDaemon - if true, all ThreadPool.PooledThreads in this ThreadPool will be daemon ThreadPool.PooledThreads
Throws:
IllegalArgumentException - if max < 0
Method Detail

init

private final void init(int max,
                        boolean isDaemon)
                 throws IllegalArgumentException
Initializes this ThreadPool so that it will permit only the supplied number of ThreadPool.PooledThreads to exist.
Parameters:
max - the maximum allowed number of ThreadPool.PooledThreads
isDaemon - if true, all ThreadPool.PooledThreads in this ThreadPool will be daemon ThreadPool.PooledThreads
Throws:
IllegalArgumentException - if max < 0

setTimeout

public void setTimeout(long timeout)
Sets the timeout (in milliseconds) for getting ThreadPool.PooledThreads from this ThreadPool or for closing this ThreadPool. A negative value specifies an infinite timeout. Calling the start(Runnable, long) method (or any variation that takes a timeout argument) will override this setting.
Parameters:
timeout - the new timeout value

getTimeout

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

setIdleTimeout

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

getIdleTimeout

public long getIdleTimeout()
Returns the idle timeout (in milliseconds) for ThreadPool.PooledThreads to exit. The default value is negative, which indicates that idle ThreadPool.PooledThreads 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 this ThreadPool. The default value is that of the thread that created this ThreadPool.

setPriority

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

getMaximumAllowed

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

getAvailableCount

public int getAvailableCount()
Returns:
The number of currently available ThreadPool.PooledThreads in this ThreadPool.

getPooledCount

public int getPooledCount()
Returns:
The total number of ThreadPool.PooledThreads in this ThreadPool that are either available or in use.

getThreadCount

public int getThreadCount()
Returns:
The total number of ThreadPool.PooledThreads in this ThreadGroup. Delegates internally by default to the activeCount() method.

getAllThreads

public Thread[] getAllThreads()
Returns:
Each thread that is active in this entire ThreadGroup

sort

private Thread[] sort(Thread[] threads)

start

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

start

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

start

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

start

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

start0

private Thread start0(Runnable target,
                      long timeout,
                      String name)
               throws NoThreadException,
                      InterruptedException

isClosed

public boolean isClosed()

close

public void close()
           throws InterruptedException
Closes down all the ThreadPool.PooledThreads in this ThreadPool as they become available. This method may block forever if any ThreadPool.PooledThreads are never returned to this ThreadPool.

close

public void close(long timeout)
           throws InterruptedException
Closes down all the ThreadPool.PooledThreads in this ThreadPool as they become available. If all the ThreadPool.PooledThreads cannot become available within the specified timeout, any active ThreadPool.PooledThreads not yet returned to this ThreadPool are interrupted.
Parameters:
timeout - milliseconds to wait before unavailable ThreadPool.PooledThreads are interrupted. If zero, don't wait at all. If negative, wait forever.

startThread

private ThreadPool.PooledThread startThread(Runnable target,
                                            String name)

ensureOpen

private void ensureOpen()
                 throws ThreadPoolClosedException

threadAvailable

void threadAvailable(ThreadPool.PooledThread thread)

threadExiting

void threadExiting(ThreadPool.PooledThread thread)

foundry-0.9.1

Copyright © 2000 - Laird Jarrett Nelson