net.sourceforge.java.util.threads
Class ThreadPool

java.lang.Object
  extended bynet.sourceforge.java.util.threads.ThreadPool

public class ThreadPool
extends Object

Simple ThreadPool implementation

Author:
Sebastian Machhausen

Field Summary
private  int activeThreadCount
           The number of currently active Threads
private  Map activeThreads
           Maps Runnables to the Threads they are running in.
private static int DEFAULT_POOL_SIZE
           The default size to use for ThreadPool instances.
private  int size
           The maximum number of Threads in the ThreaPool.
 
Constructor Summary
ThreadPool()
           Creates a new ThreadPool with the default size.
ThreadPool(int size)
           Creates a new ThreadPool with the specified size:
 
Method Summary
 Thread getThread(Runnable runnable)
           Gets a new Thread that runs the specified Runnable from this ThreadPool.
 boolean isFreeThreadAvailable()
           Returns true if a free Thread is currently available in this ThreadPool.
 void shutdown()
           Performs a safe shutdown of all currently running Threads in this ThreadPool.
 void threadFinished(Runnable runnable)
           Notifies this ThreadPool that the specified Runnable has finished its work.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_POOL_SIZE

private static final int DEFAULT_POOL_SIZE

The default size to use for ThreadPool instances.

See Also:
Constant Field Values

activeThreadCount

private int activeThreadCount

The number of currently active Threads


size

private int size

The maximum number of Threads in the ThreaPool.


activeThreads

private Map activeThreads

Maps Runnables to the Threads they are running in.

Constructor Detail

ThreadPool

public ThreadPool()

Creates a new ThreadPool with the default size.

See Also:
ThreadPool(int), DEFAULT_POOL_SIZE

ThreadPool

public ThreadPool(int size)

Creates a new ThreadPool with the specified size:

Parameters:
size - the size to use for the ThreadPool instance
See Also:
ThreadPool()
Method Detail

getThread

public Thread getThread(Runnable runnable)

Gets a new Thread that runs the specified Runnable from this ThreadPool.

Parameters:
runnable - the Runnable to run in the created Thread
Returns:
the created Thread

isFreeThreadAvailable

public boolean isFreeThreadAvailable()

Returns true if a free Thread is currently available in this ThreadPool.

Returns:
true if a free Thread is currently available in this ThreadPool; false otherwise

threadFinished

public void threadFinished(Runnable runnable)

Notifies this ThreadPool that the specified Runnable has finished its work. This means that the Thread it was running in is freed.

Parameters:
runnable - the Runnable that finished its work

shutdown

public void shutdown()

Performs a safe shutdown of all currently running Threads in this ThreadPool.