Dresden OCL Toolkit

tudresden.ocl.injection.reverseeng
Class ThreadPool

java.lang.Object
  extended byjava.lang.ThreadGroup
      extended bytudresden.ocl.injection.reverseeng.ThreadPool

public class ThreadPool
extends ThreadGroup

A pool of indistinguishable worker threads that can perform tasks.

A ThreadPool consists of a number of identical worker threads and a bag of tasks. Whenever a thread finishes one task it grabs the next available task out of the bag and performs it. The ThreadPool allows to add tasks to be performed, and to control the number of threads in the group.

Version:
0.1
Author:
sz9 (Steffen Zschaler)

Nested Class Summary
private static class ThreadPool.PooledThread
           
 
Field Summary
private  int m_cThreads
          The number of worker threads in this pool.
private  int m_cThreadsToAdd
          The number of threads that need to be added (or removed if negative!).
private  List m_lrTasks
          The bag of tasks implemented as a list.
private  int m_nNextID
          The next worker thread ID.
private  Object m_oTasksLock
          Monitor synchronizing access to the bag of tasks.
private  Object m_oThreadCountLock
          Monitor synchronizing access to the thread count variables.
 
Fields inherited from class java.lang.ThreadGroup
 
Constructor Summary
ThreadPool()
          Creates new ThreadPool with a default name and a default number of threads in the current thread group.
ThreadPool(String sName)
          Creates new ThreadPool with a default number of threads in the current thread group.
ThreadPool(String sName, int cInitialThreads)
          Creates new ThreadPool in the current thread group.
ThreadPool(String sName, int cInitialThreads, ThreadGroup tg)
          Creates new ThreadPool in the specified thread group.
 
Method Summary
 void addTask(Runnable rTask)
          Add a task to the bag of tasks needing to be performed.
 void addThreads(int cThreadsToAdd)
          Add the specified number of threads to the pool of worker threads.
private  void checkForDeath()
          Throws a ThreadDeath if there are still threads that need to die.
private  Runnable dequeueTask()
          Used internally by PooledThread to dequeue the next task.
private  Object getTasksLock()
           
private  Object getThreadCountLock()
           
 void removeThreads(int cThreadsToRemove)
          Mark the specified number of threads as needing to be removed.
 
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
 

Field Detail

m_oThreadCountLock

private transient Object m_oThreadCountLock
Monitor synchronizing access to the thread count variables.


m_cThreads

private int m_cThreads
The number of worker threads in this pool.


m_cThreadsToAdd

private int m_cThreadsToAdd
The number of threads that need to be added (or removed if negative!).

See Also:
addThreads(int), removeThreads(int)

m_nNextID

private int m_nNextID
The next worker thread ID.


m_oTasksLock

private transient Object m_oTasksLock
Monitor synchronizing access to the bag of tasks.


m_lrTasks

private List m_lrTasks
The bag of tasks implemented as a list.

Constructor Detail

ThreadPool

public ThreadPool()
Creates new ThreadPool with a default name and a default number of threads in the current thread group. The name is "ThreadPool". The initial number of threads is 10.


ThreadPool

public ThreadPool(String sName)
Creates new ThreadPool with a default number of threads in the current thread group. The initial number of threads is 20.


ThreadPool

public ThreadPool(String sName,
                  int cInitialThreads)
Creates new ThreadPool in the current thread group.

Parameters:
sName - the name of the ThreadPool.
cInitialThreads - the initial number of worker threads.

ThreadPool

public ThreadPool(String sName,
                  int cInitialThreads,
                  ThreadGroup tg)
Creates new ThreadPool in the specified thread group.

Parameters:
sName - the name of the ThreadPool.
cInitialThreads - the initial number of worker threads.
tg - the ThreadGroup in which the pool is to be created.
Method Detail

getThreadCountLock

private Object getThreadCountLock()

getTasksLock

private Object getTasksLock()

dequeueTask

private Runnable dequeueTask()
Used internally by PooledThread to dequeue the next task.

This can return in two cases:

  1. If the calling thread needs to die because too many threads already exist, a ThreadDeath exception is raised.
  2. If a non-null task is in the bag, it is removed from the bag and returned.


addTask

public void addTask(Runnable rTask)
Add a task to the bag of tasks needing to be performed.

Parameters:
rTask - the task to be added.

addThreads

public void addThreads(int cThreadsToAdd)
Add the specified number of threads to the pool of worker threads.

Parameters:
cThreadsToAdd - the number of threads to be added.

removeThreads

public void removeThreads(int cThreadsToRemove)
Mark the specified number of threads as needing to be removed.

Note that the threads will be killed only after they completed the current task.


checkForDeath

private void checkForDeath()
Throws a ThreadDeath if there are still threads that need to die. Assumes, the calling thread was a pooled thread that will subsequently die because of the ThreadDeath event.


Dresden OCL Toolkit

Submit a bug
Developed at the Dresden University of Technology.
This software is published under the GNU Lesser General Public License.