Class WatchableThread

  extended by java.lang.Thread
      extended by
All Implemented Interfaces:
Direct Known Subclasses:

public abstract class WatchableThread
extends java.lang.Thread

Defines the base for a thread that can have its progress tracked (in a limited way) as it processes. This thread performs an "action", which is executed separately from the main game. The thread will perform its potentially expensive operations (say, loading 150 images and 30 sound effects) while the rest of the game runs normally. The most obvious use is for loading - simply extend this class to have act() initialize a series of objects and increment amountComplete along the way, and you're golden.

Chris Molini

Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
Field Summary
protected  float progress
          How "much" of the action has been finished.
protected  float total
          Total "amount" to complete.
Fields inherited from class java.lang.Thread
Constructor Summary
Method Summary
protected abstract  void act()
          Performs the action that a WatchableThread is designed to do.
 boolean complete()
          Checks whether or not the action has finished.
 float percentComplete()
          Returns the percentage (0 to 1, not 0 to 100) of the action that is complete.
 void run()
          Runs the thread.
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, 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, start, stop, stop, suspend, toString, yield
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

Field Detail


protected float progress
How "much" of the action has been finished. Should never be greater than 'total.'


protected float total
Total "amount" to complete. This is the target mark for 'progress' to reach. The default is 100 (as in 100%) but it's more sensible for the developer to specify a number more correlated with what they are doing. Example: If the thread needs to load 87 images, 'total' could be set to 87, and 'progress' could be incremented after each image is loaded.

Constructor Detail


public WatchableThread()
Method Detail


public float percentComplete()
Returns the percentage (0 to 1, not 0 to 100) of the action that is complete.

A number between 0 and 1 that indicates how far the loading has progressed.


public boolean complete()
Checks whether or not the action has finished. It's a good idea to use this method to check whether or not its safe to interact with this Thread.

True if the action has finished, false if


protected abstract void act()
Performs the action that a WatchableThread is designed to do.


public void run()
Runs the thread. All that this does is call act() and then flag the action as completed.

Specified by:
run in interface java.lang.Runnable
run in class java.lang.Thread