Purple Code

com.purpletech.util
Class ThreadPool

java.lang.Object
  |
  +--com.purpletech.util.ThreadPool
All Implemented Interfaces:
java.lang.Runnable, Task

public class ThreadPool
extends java.lang.Object
implements Task

There are many possible implementations of a thread pool. This one takes a Runnable (or a Task) at creation time; it spawns N threads and starts all of them running inside the task. It then periodically wakes up and checks the state of all the threads. If most of them are blocked, then it will spawn another thread and run it inside the task. If most of them are idle, then it will try to kill one of them.

This class depends on the ThreadWatcher class, and hopes that the tasks will periodically inform the ThreadWatcher of their status. Otherwise it can't spawn or kill threads.

Version:
$Id: ThreadPool.java,v 1.2 2001/10/30 12:20:53 alex Exp $
Author:
Alex

Constructor Summary
ThreadPool(java.lang.String name, java.lang.Runnable task, int minThreads, int maxThreads, int priority, int priorityPool, int pause, ThreadWatcher watcherMain)
           
ThreadPool(java.lang.String name, java.lang.Runnable task, int minThreads, int maxThreads, int priority, int pause, ThreadWatcher watcherMain)
           
 
Method Summary
protected  void removeThread(java.lang.Thread t, java.lang.String msg)
          Remove this thread from the pool.
 void run()
           
 void setTask(java.lang.Runnable task)
           
 void setWatcher(ThreadWatcher w)
           
 int size()
           
 void start()
           
protected  void startThread()
          Start a new thread running in the pool.
 void stop()
           
 
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,
                  java.lang.Runnable task,
                  int minThreads,
                  int maxThreads,
                  int priority,
                  int priorityPool,
                  int pause,
                  ThreadWatcher watcherMain)

ThreadPool

public ThreadPool(java.lang.String name,
                  java.lang.Runnable task,
                  int minThreads,
                  int maxThreads,
                  int priority,
                  int pause,
                  ThreadWatcher watcherMain)
Method Detail

setWatcher

public void setWatcher(ThreadWatcher w)
Specified by:
setWatcher in interface Task

setTask

public void setTask(java.lang.Runnable task)

start

public void start()

stop

public void stop()
Specified by:
stop in interface Task

run

public void run()
Specified by:
run in interface java.lang.Runnable

size

public int size()

startThread

protected void startThread()
Start a new thread running in the pool.


removeThread

protected void removeThread(java.lang.Thread t,
                            java.lang.String msg)
Remove this thread from the pool. Usually called only by the thread itself.


Documentation generated March 5 2003