com.internationalnetwork.lang
Class DynamicThread<Target extends Runnable>

Object
  extended by Thread
      extended by com.internationalnetwork.lang.DynamicThread<Target>
All Implemented Interfaces:
Runnable

public final class DynamicThread<Target extends Runnable>
extends Thread

A DynamicThread is the Object returned by DynamicThreadPool.dispatch(). In addition to the Thread Object, it also tracks the Runnable Object and some other useful information such as the status of the thread (not to be confused with a "thread state").


Nested Class Summary
 
Nested classes/interfaces inherited from class Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static String VERSION
          Version number of this Package (read-only).
 
Fields inherited from class Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
DynamicThread(DynamicThreadPool pool, long poolID, ThreadGroup threadGroup, Target target, String threadName, long stackSize)
          Instantiates a new DynamicThread object.
 
Method Summary
 long getDispatchCount()
          Returns the number of times this DynamicThread was dispatched.
 long getExceptionCount()
          Returns the number of times an uncaught exception was thrown.
 DynamicThreadPool getPool()
          Returns the DynamicThreadPool object that instantiated this DynamicThread object and is responsible for dispatching it.
 long getPoolID()
          Returns the DynamicThread object's ID number that was assigned by the DynamicThreadPool object.
 String getStatus()
          Returns the status of the DynamicThread.
 Target getTarget()
          Returns the Target object that was instantiated.
 Thread getThread()
          Returns the java.lang.Thread superclass object, but only after the thread has been started.
 long getThreadID()
          Returns the Thread object's ID number that was assigned by the JVM.
 boolean isRunning()
          Indicates if this run() method is active.
 boolean isTerminating()
          Indicates if this DynamicThread is flagged for termination.
 void run()
          Don't call this method directly, use start() to begin execution instead.
 String setStatus(String newStatus)
          Updates the status of the DynamicThread.
 void start()
          Begins thread execution.
 void terminate()
          Causes the thread to terminate before re-run, no matter how many re-runs may be remaining.
 
Methods inherited from class Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
Version number of this Package (read-only).

See Also:
Constant Field Values
Constructor Detail

DynamicThread

public DynamicThread(DynamicThreadPool pool,
                     long poolID,
                     ThreadGroup threadGroup,
                     Target target,
                     String threadName,
                     long stackSize)
Instantiates a new DynamicThread object. This constructor isn't intended for general purpose use, as it was designed specifically for exclusive use by the DynamicThreadPool class.

The target is the Runnable (or ReRunnable) object. If your compiler generates the "type parameter target is not within its bound" error message, the most likely cause is inconsistent use of Runnable and ReRunnable in various declarations -- determine which one is most appropriate for your DynamicThreadPool object and use it consistently.

Parameters:
pool - The DynamicThreadPool object that created this DynamicThread
threadGroup - The thread group that will adopt the new Thread object
threadName - The name to assign to the new Thread object
target - The Runnable (or ReRunnable) Class to be instantiated for the new Thread object
stackSize - The size of the stack for use by the new Thread object
Throws:
IllegalAccessException - if a public constructor (with no arguments) in/or the target class is not accessible
InstantiationException - if a public constructor (with no arguments) in the target class isn't defined
Method Detail

getDispatchCount

public long getDispatchCount()
Returns the number of times this DynamicThread was dispatched. This method is only applicable when the Target implements the ReRunnable interface.

Returns:
dispatch count

getExceptionCount

public long getExceptionCount()
Returns the number of times an uncaught exception was thrown. This method is only applicable when the Target implements the ReRunnable interface.

Returns:
exception count

getPool

public DynamicThreadPool getPool()
Returns the DynamicThreadPool object that instantiated this DynamicThread object and is responsible for dispatching it.

Returns:
com.internationalnetwork.lang.DynamicThreadPool object

getPoolID

public long getPoolID()
Returns the DynamicThread object's ID number that was assigned by the DynamicThreadPool object. This number is like a thread dispatch counter that the pool increments every time it creates a new DynamicThread, and is never re-used (unless the total number of threads created exceeds the limit for a long integer, in which case the counting will begin all over again).

Returns:
long integer ID number associated with this DynamicThread

getStatus

public String getStatus()
Returns the status of the DynamicThread.

Returns:
Current status

getTarget

public Target getTarget()
Returns the Target object that was instantiated.

This is a very important method because it provides access to the Runnable target class that was instantiated by the DynamicThread constructor and specified as the Runnable target when the Thread was instantiated.

Usage example: MyThread myThread = (MyThread)dynamicThread.getTarget();

It is assumed that certain preparations will be made by way of various public methods, etc., prior to starting the thread. For example, threads dispatched for inbound TCP/IP connections would need to have the relevant Socket object, so a typical modifier method "myThread.setSocket(Socket)" would be used before dynamicThread.start().

Returns:
The Target object type wrapped in the Runnable class

getThread

public Thread getThread()
Returns the java.lang.Thread superclass object, but only after the thread has been started. If the thread was not started, NULL is returned.

Returns:
java.lang.Thread

getThreadID

public long getThreadID()
Returns the Thread object's ID number that was assigned by the JVM. This number is re-used by the JVM, and should only be relied upon for uniqueness where it use required for differentiating between active processes.

Essentially, this is the same as calling Thread.getCurrentThread().getId() but with overhead reduced.

This is a convenience method that both simplifies code (to help eliminate calls like "getThread().getId()"), and reduces CPU utilization (because the Thread.getId() method is only called once, with repeat calls merely referencing a cached version of the Thread ID number).

Returns:
Positive long integer

isRunning

public boolean isRunning()
Indicates if this run() method is active.

This is intended for internal use. It will probably always return true.

Returns:
boolean True indicates that thread is active, False indicates that the thread has completed

isTerminating

public boolean isTerminating()
Indicates if this DynamicThread is flagged for termination.

This is intended for internal use. It will probably always return false.

Returns:
boolean True indicates that thread will be terminated (if it isn't already), False indicates that the thread could still be re-run

run

public void run()
Don't call this method directly, use start() to begin execution instead.

The DynamicThread.start() method performs additional pool clean-up when the thread terminates.

Specified by:
run in interface Runnable
Overrides:
run in class Thread
Throws:
IllegalThreadStateException - if the thread was previously started (threads can't be re-started)

setStatus

public String setStatus(String newStatus)
Updates the status of the DynamicThread. Synchronization is used to facilitate thread-safe updates from multiple threads.

Returns:
Previous status

start

public void start()
Begins thread execution. Don't start the thread directly, use this method to start it correctly.

Overrides:
start in class Thread

terminate

public void terminate()
Causes the thread to terminate before re-run, no matter how many re-runs may be remaining.

If the thread is running, then termination will occur after completion.