com.brackeen.javagamebook.util
Class ThreadPool

java.lang.Object
  extended by java.lang.ThreadGroup
      extended by com.brackeen.javagamebook.util.ThreadPool
All Implemented Interfaces:
java.lang.Thread.UncaughtExceptionHandler
Direct Known Subclasses:
SoundManager

public class ThreadPool
extends java.lang.ThreadGroup

A thread pool is a group of a limited number of threads that are used to execute tasks.


Nested Class Summary
private  class ThreadPool.PooledThread
          A PooledThread is a Thread in a ThreadPool group, designed to run tasks (Runnables).
 
Field Summary
private  boolean isAlive
           
private  java.util.LinkedList taskQueue
           
private  int threadID
           
private static int threadPoolID
           
 
Constructor Summary
ThreadPool(int numThreads)
          Creates a new ThreadPool.
 
Method Summary
 void close()
          Closes this ThreadPool and returns immediately.
protected  java.lang.Runnable getTask()
           
 void join()
          Closes this ThreadPool and waits for all running threads to finish.
 void runTask(java.lang.Runnable task)
          Requests a new task to run.
protected  void threadStarted()
          Signals that a PooledThread has started.
protected  void threadStopped()
          Signals that a PooledThread has stopped.
 
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

isAlive

private boolean isAlive

taskQueue

private java.util.LinkedList taskQueue

threadID

private int threadID

threadPoolID

private static int threadPoolID
Constructor Detail

ThreadPool

public ThreadPool(int numThreads)
Creates a new ThreadPool.

Parameters:
numThreads - The number of threads in the pool.
Method Detail

runTask

public void runTask(java.lang.Runnable task)
Requests a new task to run. This method returns immediately, and the task executes on the next available idle thread in this ThreadPool.

Tasks start execution in the order they are received.

Parameters:
task - The task to run. If null, no action is taken.
Throws:
java.lang.IllegalStateException - if this ThreadPool is already closed.

getTask

protected java.lang.Runnable getTask()
                              throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

close

public void close()
Closes this ThreadPool and returns immediately. All threads are stopped, and any waiting tasks are not executed. Once a ThreadPool is closed, no more tasks can be run on this ThreadPool.


join

public void join()
Closes this ThreadPool and waits for all running threads to finish. Any waiting tasks are executed.


threadStarted

protected void threadStarted()
Signals that a PooledThread has started. This method does nothing by default; subclasses should override to do any thread-specific startup tasks.


threadStopped

protected void threadStopped()
Signals that a PooledThread has stopped. This method does nothing by default; subclasses should override to do any thread-specific cleanup tasks.