objectdraw
Class ActiveObject

java.lang.Object
  extended by java.lang.Thread
      extended by objectdraw.ActiveObject
All Implemented Interfaces:
Runnable

public class ActiveObject
extends Thread

The ActiveObject class allows you to create a separate "thread" of a program that runs its own code at its own pace, independently of other code that is running. You can think of each instance of ActiveObject as being a worker helping to do some task; there may be many workers working at the same time, each doing their own job.

You will use ActiveObject by defining a class that extends it, and writing in that class a run() method containing the work you want that object to do:

public class NewWorkerClass extends ActiveObject {
   public void run() {
      // Your code here
   }
}


Once you have defined a new class of ActiveObject, you can then create new workers and get them started on their work by calling their start() method:

NewWorkerClass worker = new NewWorkerClass();
worker.start();


Typically, instead of doing all its work at once, you will will want the worker to pause after each step, so that you can see it gradually doing something, like moving a ball across the screen a little bit at a time. The pause() method will tell an ActiveObject to wait for some amount of time, allowing other ActiveObjects to work or allowing mouse and keyboard events to be processed.

Author:
Russell Zahniser (russell@zahniser.net)

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ActiveObject()
          Create a new ActiveObject.
ActiveObject(Runnable target)
          Create a new ActiveObject that will run, not its own run() method, but the run() method of some other object that implements the Runnable interface.
ActiveObject(Runnable target, String name)
          Create a new ActiveObject that will run, not its own run() method, but the run() method of some other object that implements the Runnable interface.
ActiveObject(String name)
          Create a new ActiveObject whose thread will show up in the debugger with the given name.
 
Method Summary
 void kill()
          Kill this ActiveObject, stopping whatever it is doing.
static void killAll()
          Call the kill() method of every ActiveObject currently running.
static void pause(double millis)
          Pause for at least the number of milliseconds specified.
static void pause(long millis)
          Pause for at least the number of milliseconds specified.
static void pause(long millis, int nanos)
          Pause for at least the number of milliseconds specified, plus the specified number of nanoseconds.
 void run()
          This is the method to override to specify what work this ActiveObject will do when it is start()ed.
 void start()
          Call this method to start this ActiveObject running.
static void yield()
          If any other threads are waiting for a chance to do something, pause until they have had a turn.
 
Methods inherited from class java.lang.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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

ActiveObject

public ActiveObject()
Create a new ActiveObject. The name of its thread in the debugger will be "ActiveObject" followed by a unique id number.


ActiveObject

public ActiveObject(Runnable target)
Create a new ActiveObject that will run, not its own run() method, but the run() method of some other object that implements the Runnable interface.

Parameters:
target - An object whose run() method will be called when this ActiveObject is start()ed, or null if it should run its own run() method.

ActiveObject

public ActiveObject(Runnable target,
                    String name)
Create a new ActiveObject that will run, not its own run() method, but the run() method of some other object that implements the Runnable interface.

Parameters:
target - An object whose run() method will be called when this ActiveObject is start()ed, or null if it should run its own run() method.
name - Thread name to represent this ActiveObject in the debugger.

ActiveObject

public ActiveObject(String name)
Create a new ActiveObject whose thread will show up in the debugger with the given name.

Parameters:
name - Name of thread to be shown in debugger
Method Detail

kill

public void kill()
Kill this ActiveObject, stopping whatever it is doing. Because of the inherent danger in stopping a thread in the middle of running code, this method will merely set a flag saying this thread has been killed, which will then cause the thread to die the next time it calls ActiveObject.pause() or ActiveObject.yield().

In order for this method to function properly, your ActiveObject ought to call sleep() or yield() appropriately rather than just running constantly. Also, if you catch Throwable, Error, or ThreadDeath you will have to be sure to throw it again.


killAll

public static void killAll()
Call the kill() method of every ActiveObject currently running. If your program is running as an applet in a web browser, this method is automatically called when the user navigates away from the page containing your applet. This prevents all your ActiveObjects from just merrily churning away with no applet left to draw on.


pause

public static void pause(double millis)
Pause for at least the number of milliseconds specified. A millisecond is a thousandth (1/1000) of a second. So, pause(1000, 0) would pause for one second.

Parameters:
millis - number of milliseconds to pause for

pause

public static void pause(long millis)
Pause for at least the number of milliseconds specified. A millisecond is a thousandth (1/1000) of a second. So, pause(1000, 0) would pause for one second.

Parameters:
millis - number of milliseconds to pause for

pause

public static void pause(long millis,
                         int nanos)
Pause for at least the number of milliseconds specified, plus the specified number of nanoseconds. A millisecond is a thousandth (1/1000) of a second, and a nanosecond is a billionth (1/1,000,000,000) of a second. So, pause(1000, 0) would pause for one second, and pause(0, 500000) would pause for half a millisecond.

Parameters:
millis - number of milliseconds to pause for
nanos - number of additional nanoseconds to pause for

run

public void run()
This is the method to override to specify what work this ActiveObject will do when it is start()ed.

Specified by:
run in interface Runnable
Overrides:
run in class Thread

start

public void start()
Call this method to start this ActiveObject running. This method can only be called once for a particular object: even if it has finished its previous run, you cannot start it again. Instead, create and start a new instance.

Overrides:
start in class Thread

yield

public static void yield()
If any other threads are waiting for a chance to do something, pause until they have had a turn.