javatools.thread
Class ThreadPool

java.lang.Object
  |
  +--javatools.thread.ThreadPool

public class ThreadPool
extends java.lang.Object

Implements a pool of threads. Only a limited number of threads can execute at once. If you try to exceed the thread limit you will block waiting. This avoids overloading a machine with threads.


Field Summary
(package private)  java.util.List jobs
           
(package private)  java.lang.ThreadGroup threadGroup
           
(package private)  java.util.List threads
           
 
Constructor Summary
ThreadPool(java.lang.String name, int maxThreads)
           
 
Method Summary
 boolean anyReadyThreads()
          Check whether any pool threads are currently available for use.
protected  void finalize()
           
(package private)  java.lang.Runnable getJob()
          Get a job to execute and remove it from the job queue.
 void join()
          Wait for all threads to complete.
 int numberOfActiveThreads()
          How many threads are active right now? This number can change at any time, so it is not particularly useful except for providing interesting diagnostics.
 int numberOfReadyThreads()
           
 void queueJob(java.lang.Runnable runnable)
          Execute the given procedure when a thread slot becomes available.
 void shutdown()
          Shutdown all pool threads safely.
 void startNow(java.lang.Runnable runnable)
          Start a job now.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

threads

java.util.List threads

threadGroup

java.lang.ThreadGroup threadGroup

jobs

java.util.List jobs
Constructor Detail

ThreadPool

public ThreadPool(java.lang.String name,
                  int maxThreads)
Parameters:
name - the name of the ThreadGroup
maxThreads - the maximum number of threads we allow at once.
Method Detail

getJob

java.lang.Runnable getJob()
Get a job to execute and remove it from the job queue.

Returns:
Job that needs executing.

startNow

public void startNow(java.lang.Runnable runnable)
Start a job now. If no threads are available, start a new thread to make sure it gets going now.

Parameters:
runnable -

queueJob

public void queueJob(java.lang.Runnable runnable)
Execute the given procedure when a thread slot becomes available.

Parameters:
runnable - the runnable that represents the work that shall be done.
Returns:
false if there was no slot available. Try again later.

join

public void join()
Wait for all threads to complete. Make sure you call shutdown() first.


numberOfActiveThreads

public int numberOfActiveThreads()
How many threads are active right now? This number can change at any time, so it is not particularly useful except for providing interesting diagnostics.

Returns:
Description of the Returned Value

anyReadyThreads

public boolean anyReadyThreads()
Check whether any pool threads are currently available for use. If the next available thread is unstarted we start it. This allows us to start the pool on an as-needed basis. If true is returned there is a thread available. If false is returned there is not a thread available unless one finished half-way through the function.

Returns:
whether there are currently available threads.

numberOfReadyThreads

public int numberOfReadyThreads()

shutdown

public void shutdown()
Shutdown all pool threads safely. This does not wait for them to complete. Call join() if you want that.


finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object
java.lang.Throwable