Voyager ORB

com.objectspace.lib.thread
Class ThreadPool

com.objectspace.lib.thread.ThreadPool

public class ThreadPool

A ThreadPool allows threads to be reused.

Each thread pool contains an internal cache which is set to the initial capacity. When a thread is requested using assignThreadTo(), a new thread is created only if one cannot be reactivated from the cache. When a thread that was allocated using assignThreadTo() completes, it is deactivated and added to the cache.

If a thread is returned to the pool and there are no waiting requests, it is placed in a cache.

Version:
3.0

Constructor Summary
ThreadPool()
          Construct a thread pool with 0 initial threads, a creation rate of 1, and no specified maxCapacity.
ThreadPool(java.lang.ThreadGroup threadGroup)
          Construct a thread pool with 0 initial threads, a creation rate of 1, and no specified maxCapacity.
ThreadPool(java.lang.ThreadGroup threadGroup, int initialCapacity, int creationRate, int maxCapacity)
          Construct a thread pool with the specified parameters.
 
Method Summary
 void assignThreadTo(java.lang.Runnable runnable)
          Assign a thread with no name and current priority to the runnable object and start it running immediately.
 void assignThreadTo(java.lang.Runnable runnable, java.lang.String name)
          Assign a thread with the specified name and priority to the runnable object and start it running immediately.
 void assignThreadTo(java.lang.Runnable runnable, java.lang.String name, int priority)
          Assign a thread with the specified name and priority to the runnable object and start it running immediately.
 int getCreationRate()
          Return the rate at which resources are created.
 int getFreeThreadCount()
          Return the number of threads in the free pool.
 int getMaxIdleThreads()
          Return the maximum number of threads I can cache.
 int getTotalThreadCount()
          Return the total number of threads that are being managed by this thread pool.
 void setCreationRate(int creationRate)
          Set the rate at which resources are created.
 void setMaxIdleThreads(int maxIdleThreads)
          Set the maximum number of threads I can cache.
 

Constructor Detail

ThreadPool

public ThreadPool()
Construct a thread pool with 0 initial threads, a creation rate of 1, and no specified maxCapacity. Places all threads in the default ThreadGroup.

ThreadPool

public ThreadPool(java.lang.ThreadGroup threadGroup)
Construct a thread pool with 0 initial threads, a creation rate of 1, and no specified maxCapacity. Places all threads in the specified ThreadGroup.

ThreadPool

public ThreadPool(java.lang.ThreadGroup threadGroup,
                  int initialCapacity,
                  int creationRate,
                  int maxCapacity)
Construct a thread pool with the specified parameters.
Method Detail

getTotalThreadCount

public int getTotalThreadCount()
Return the total number of threads that are being managed by this thread pool. This number includes the number of threads in the free pool plus the number of threads currently doing work.

getFreeThreadCount

public int getFreeThreadCount()
Return the number of threads in the free pool. That is, return the number of threads not doing work.

setMaxIdleThreads

public void setMaxIdleThreads(int maxIdleThreads)
Set the maximum number of threads I can cache. If the current cache is larger than the specified size, threads are removed from the idle cache and released.
Parameters:
maxIdleThreads - the maximum number of threads I can cache.
Throws:
java.lang.IllegalArgumentException - maxIdleThreads < 0

getMaxIdleThreads

public int getMaxIdleThreads()
Return the maximum number of threads I can cache.

setCreationRate

public void setCreationRate(int creationRate)
Set the rate at which resources are created. If the pool is empty and a new thread needs to be created, the thread pool will actually pre-allocate the number specified by creationRate.

getCreationRate

public int getCreationRate()
Return the rate at which resources are created. If the pool is empty and a new thread needs to be created, the thread pool will actually pre-allocate the number specified by this value.

assignThreadTo

public void assignThreadTo(java.lang.Runnable runnable,
                           java.lang.String name,
                           int priority)
Assign a thread with the specified name and priority to the runnable object and start it running immediately. If there is an idle thread in the cache, reallocate it, otherwise allocate a new thread. When the thread completes, add it back into the cache unless the cache already contains the maximum number of idle threads.
Parameters:
runnable - The object whose run() method will be executed.
name - The name to be given to the thread.
priority - The priority to be given to the thread.

assignThreadTo

public void assignThreadTo(java.lang.Runnable runnable,
                           java.lang.String name)
Assign a thread with the specified name and priority to the runnable object and start it running immediately. If there is an idle thread in the cache, reallocate it, otherwise allocate a new thread. When the thread completes, add it back into the cache unless the cache already contains the maximum number of idle threads.
Parameters:
runnable - The object whose run() method will be executed.
name - The name to be given to the thread.

assignThreadTo

public void assignThreadTo(java.lang.Runnable runnable)
Assign a thread with no name and current priority to the runnable object and start it running immediately. If there is an idle thread in the cache, reallocate it, otherwise allocate a new thread. When the thread completes, add it back into the cache unless the cache already contains the maximum number of idle threads.
Parameters:
runnable - The object whose run() method will be executed.

ObjectSpace Inc.

(c) Copyright 1997-1999 ObjectSpace, Inc.
14850 Quorum Drive, Suite 500
Dallas, Texas 75240