com.objectwave.utility
Class ThreadPoolManager

java.lang.Object
  |
  +--com.objectwave.utility.ThreadPoolManager

public class ThreadPoolManager
extends java.lang.Object

A simple thread pool implementation. With only two new lines of code, you could manage all of your threads. Original Code new Thread(r).start() New code ThreadPoolManager mgr = new ThreadPoolManager(10); mgr.start(r);

Version:
$Id: ThreadPoolManager.java,v 1.1.1.1 2001/02/13 20:47:02 dhoag Exp $
Author:
David Hoag

Nested Class Summary
static class ThreadPoolManager.Test
          Unit Tests
 
Field Summary
protected  com.objectwave.utility.RunnableQueue waitingRunnables
           
 
Constructor Summary
ThreadPoolManager(int numOfThread)
           
 
Method Summary
 java.lang.Runnable[] clearQueue()
          Remove all runnable objects waiting to execute.
protected  void finalize()
          If we get garbaged collected, shutdown the thread pool.
 java.lang.ThreadGroup getThreadGroup()
          Get the threadGroup that contains all of the threads in the thread pool.
 boolean isShutdown()
          If the thread pool is being shutdown, this method will return true.
 java.lang.Runnable[] shutdown()
          Gracefully terminate the ThreadPool.
 void start(java.lang.Runnable runnable)
          Enqueue the runnable.
protected  boolean threadWaiting(com.objectwave.utility.ManagedThread mt)
          Called by the managed thread when it is done processing a runnable.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

waitingRunnables

protected com.objectwave.utility.RunnableQueue waitingRunnables
Constructor Detail

ThreadPoolManager

public ThreadPoolManager(int numOfThread)
Parameters:
numOfThread -
Method Detail

getThreadGroup

public java.lang.ThreadGroup getThreadGroup()
Get the threadGroup that contains all of the threads in the thread pool.

Returns:
The ThreadGroup value

isShutdown

public boolean isShutdown()
If the thread pool is being shutdown, this method will return true. Once this method returns true, this thread pool can no longer be used.

Returns:
The Shutdown value
See Also:
shutdown()

clearQueue

public java.lang.Runnable[] clearQueue()
Remove all runnable objects waiting to execute.

Returns:
Runnable [] The runnables who will never see the light of day

shutdown

public java.lang.Runnable[] shutdown()
Gracefully terminate the ThreadPool. No threads are 'shot'. They are given a chance to end normally. There maybe runnables in the queue that will never see the light of day. All threads will be terminated prior to the return of this method. The pool becomes unusable and must be discarded.

Returns:
Runnable [] The runnables who will never see the light of day

start

public void start(java.lang.Runnable runnable)
Enqueue the runnable. When a thread becomes available, the parameter runnable object will be executed. A 'notify' is called before this method is exited. This will wake any waiting threads.

Parameters:
runnable -

threadWaiting

protected boolean threadWaiting(com.objectwave.utility.ManagedThread mt)
                         throws java.lang.InterruptedException
Called by the managed thread when it is done processing a runnable. The managedThread goes into a wait state on the ThreadPool. When start(Runnable) is called, the 'notify' method on the ThreadPool is called. Some waiting thread will go into action, pick off an element from the list of waiting Runnables and begin execution. Upon completion, this method is called again.

Parameters:
mt -
Returns:
Throws:
java.lang.InterruptedException
See Also:
ManagedThread

finalize

protected void finalize()
                 throws java.lang.Throwable
If we get garbaged collected, shutdown the thread pool.

Overrides:
finalize in class java.lang.Object
Throws:
java.lang.Throwable