tyrex.util
Class BackgroundThread

java.lang.Object
  extended byjava.lang.Thread
      extended bytyrex.util.BackgroundThread
All Implemented Interfaces:
java.lang.Runnable

public final class BackgroundThread
extends java.lang.Thread

This thread allows a runnable to run continously until the runnable is garbage collected.

The thread waits for n number of milliseconds before running the runnable. The wait-run execution occurs in an infinite loop. If the runnable is garbage collected then the thread exits its loop and stops running.

For best results the runnable should not have an embedded loop. Obviously if the runnable has an infinite loop then this thread never ends by itself.

Author:
Riad Mohammed

Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
BackgroundThread(java.lang.Runnable runnable, long wait)
          Create the BackgroundThread.
BackgroundThread(java.lang.Runnable runnable, long wait, java.lang.String name)
          Create the BackgroundThread.
BackgroundThread(java.lang.Runnable runnable, long wait, java.lang.ThreadGroup threadGroup)
          Create the BackgroundThread.
BackgroundThread(java.lang.Runnable runnable, long wait, java.lang.ThreadGroup threadGroup, java.lang.String name)
          Create the BackgroundThread.
 
Method Summary
 boolean getExitOnInterrupt()
          Return true if the background thread exits when it is interrupted.
 long getWait()
          Return the time in milliseconds to wait before the runnable is run.
 void run()
          Run the runnable.
 void setExitOnInterrupt(boolean exitOnInterrupt)
          Tell the background thread to exit or not, when it is interrupted.
 void setWait(long wait)
          Set the time in milliseconds to wait before the the runnable is run.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BackgroundThread

public BackgroundThread(java.lang.Runnable runnable,
                        long wait)
Create the BackgroundThread.

Parameters:
runnable - the runnable to run
wait - the time to wait before the runnable is run.

BackgroundThread

public BackgroundThread(java.lang.Runnable runnable,
                        long wait,
                        java.lang.String name)
Create the BackgroundThread.

Parameters:
runnable - the runnable to run
wait - the time to wait before the runnable is run.
name - the name of the Thread. Cannot be null.

BackgroundThread

public BackgroundThread(java.lang.Runnable runnable,
                        long wait,
                        java.lang.ThreadGroup threadGroup)
Create the BackgroundThread.

Parameters:
runnable - the runnable to run
wait - the time to wait before the runnable is run.
threadGroup - the thread group of the new thread. Can br null.

BackgroundThread

public BackgroundThread(java.lang.Runnable runnable,
                        long wait,
                        java.lang.ThreadGroup threadGroup,
                        java.lang.String name)
Create the BackgroundThread.

Parameters:
runnable - the runnable to run
wait - the time to wait before the runnable is run.
threadGroup - the thread group of the new thread. Can br null.
name - the name of the Thread. Cannot be null.
Method Detail

getExitOnInterrupt

public boolean getExitOnInterrupt()
Return true if the background thread exits when it is interrupted.

Returns:
true if the background thread exits when it is interrupted.

setExitOnInterrupt

public void setExitOnInterrupt(boolean exitOnInterrupt)
Tell the background thread to exit or not, when it is interrupted.

Parameters:
exitOnInterrupt - True if the background thread exits when it is interrupted

getWait

public long getWait()
Return the time in milliseconds to wait before the runnable is run.

Returns:
the wait time in milliseconds

setWait

public void setWait(long wait)
Set the time in milliseconds to wait before the the runnable is run.

Parameters:
wait - the wait time in milliseconds. Must be greater than 0.

run

public void run()
Run the runnable.

If the runnable has been garbage collected the thread ends.

The thread sleeps the prescribed number of seconds before running the runnable. The wait-run execution occurs in an infinite loop



Original code is Copyright (c) 1999-2001, Intalio, Inc. All Rights Reserved. Contributions by MetaBoss team are Copyright (c) 2003-2005, Softaris Pty. Ltd. All Rights Reserved.