quicktime.app.time
Class TaskThread

java.lang.Object
  |
  +--quicktime.app.time.TaskThread
All Implemented Interfaces:
Collection, java.lang.Runnable

public class TaskThread
extends java.lang.Object
implements java.lang.Runnable, Collection

The TaskThread class maintains a table of Taskable objects that will periodically have their task method called. Each time an instance of this class is created a new thread is spawned when the start method of the TaskThread object is called. The thread is stopped when the stop method is called. Taskable objects can be added or removed from TaskThreads at the applications discretion.

When the thread is running it calls its Taskable member's task method. When it has completed a single iteration of its members it will sleep for the specified sleepTime and then go through the process again until the thread is stopped.

A TaskThread object must be explicitly started - just adding a Taskable object to the collection is not sufficient to start the thread. However, once all of the Taskable objects are removed from the collection the thread will stop. To ensure that adding a Taskable object to a TaskThread will also have the thread started the addAndStart method can be used.

If a member's task() method throws an Exception it is uncerimoniously removed from the TaskThread collection - no notification is given of such an occurance.


Constructor Summary
TaskThread(java.lang.String tName)
          Creates a TaskThread object.
TaskThread(java.lang.String tName, int sleepTime)
          Creates a TaskThread object.
 
Method Summary
 void addAndStart(Taskable t)
          This method will add a taskable client to the list.
 boolean addMember(java.lang.Object member)
          This method will add a taskable client to the list.
protected  void finalize()
           
 int getPriority()
          Returns the current priority of the task thread.
 int getSleepTime()
          The time that the thread will sleep.
 boolean hasMember(java.lang.Object object)
          Return true if the Taskable object is currently a member of the task list.
 boolean isAlive()
          Returns the alive state of the thread.
 boolean isAppropriate(java.lang.Object object)
          Returns true if the object is an instance of the Taskable interface.
 boolean isEmpty()
          Returns true if there are no Taskable objects in the collection
static void killAllThreads()
           
 java.util.Enumeration members()
          Returns an Enumeration for the current Taskable members of the collection.
 void removeAll()
          Remove all task objects from the task list and stops the task thread.
 void removeMember(java.lang.Object member)
          Removes the Taskable object from the collection.
 void resume()
          Resumes the execution of the task thread.
 void run()
          Calls task on each of the task object in the internal task list.
 void setPriority(int newPriority)
          Sets the priority of the thread.
 void setSleepTime(int newTime)
          The sleepTime for the thread.
 int size()
          Return the number of task current objects.
 void start()
          This will create a thread and start it running, calling task on each of the items you have added to the TaskThread object.
 void stop()
          This will destroy the thread and end the periodic calling of task objects.
 void suspend()
          Suspends the execution of the task thread.
 java.lang.String toString()
          String representation of the class.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TaskThread

public TaskThread(java.lang.String tName)
Creates a TaskThread object. When the object's start method is called the supplied name will be the name of the thread that is created by the task thread object. The default sleep time for a TaskThread's thread is 100 msecs.
Parameters:
tName - the name of the thread

TaskThread

public TaskThread(java.lang.String tName,
                  int sleepTime)
Creates a TaskThread object. When the object's start method is called the supplied name will be the name of the thread that is created by the task thread object.
Parameters:
sleepTime - sets the sleep time that will be used when the thread starts
Method Detail

killAllThreads

public static final void killAllThreads()

getSleepTime

public int getSleepTime()
The time that the thread will sleep.

setSleepTime

public void setSleepTime(int newTime)
The sleepTime for the thread. If the sleepTime is set to zero, the thread will only yield (but not sleep), otherwise it sleeps for the given time.

addMember

public boolean addMember(java.lang.Object member)
This method will add a taskable client to the list. It will not alter the state of the thread.
Specified by:
addMember in interface Collection
Parameters:
t - a taskable client that wants to be called periodically

isAppropriate

public boolean isAppropriate(java.lang.Object object)
Returns true if the object is an instance of the Taskable interface.
Specified by:
isAppropriate in interface Collection
Returns:
a boolean

members

public java.util.Enumeration members()
Returns an Enumeration for the current Taskable members of the collection.
Specified by:
members in interface Collection
Returns:
an Enumeration

addAndStart

public void addAndStart(Taskable t)
This method will add a taskable client to the list. If the thread was stopped (because there were no objects to task) this will start the thread up.
Parameters:
t - the Taskable object to add to the collection.

removeMember

public void removeMember(java.lang.Object member)
Removes the Taskable object from the collection. If this would leave the collection empty then the internal thread is stopped.
Specified by:
removeMember in interface Collection
Parameters:
t - a task client that wants to be called periodically

removeAll

public void removeAll()
Remove all task objects from the task list and stops the task thread.

size

public int size()
Return the number of task current objects.
Specified by:
size in interface Collection
Returns:
an int

hasMember

public boolean hasMember(java.lang.Object object)
Return true if the Taskable object is currently a member of the task list.
Specified by:
hasMember in interface Collection
Parameters:
object - the object to test if a member
Returns:
true if the object is a member

isEmpty

public boolean isEmpty()
Returns true if there are no Taskable objects in the collection
Specified by:
isEmpty in interface Collection
Returns:
a boolean

start

public void start()
This will create a thread and start it running, calling task on each of the items you have added to the TaskThread object. If a thread is already active this call has no effect

stop

public void stop()
This will destroy the thread and end the periodic calling of task objects. You can restart this object later by calling start. The priority and sleepTime will be persistent across start and stop calls, as will the task objects themselves.

suspend

public void suspend()
Suspends the execution of the task thread.

resume

public void resume()
Resumes the execution of the task thread.

isAlive

public boolean isAlive()
Returns the alive state of the thread.

getPriority

public int getPriority()
Returns the current priority of the task thread.
Returns:
the task thread priority or PRIORITY_NOT_SET if the TaskThread object has not had a priority explicitly set.

setPriority

public void setPriority(int newPriority)
Sets the priority of the thread.
Parameters:
newPriority - the new priority of the thread or any consequent threads created.

run

public void run()
Calls task on each of the task object in the internal task list. If an object's task call fails it is removed from the collection.
Specified by:
run in interface java.lang.Runnable

finalize

protected void finalize()
                 throws java.lang.Throwable
Overrides:
finalize in class java.lang.Object

toString

public java.lang.String toString()
String representation of the class.
Overrides:
toString in class java.lang.Object