com.brackeen.javagamebook.util
Class ThreadPool

java.lang.Object
  extended byjava.lang.ThreadGroup
      extended bycom.brackeen.javagamebook.util.ThreadPool
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.


Constructor Summary
ThreadPool(int numThreads)
          Creates a new ThreadPool.
 
Method Summary
 void close()
          Closes this ThreadPool and returns immediately.
 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.
 
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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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.

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.