java.lang.Objectjava.lang.Thread
objectdraw.ActiveObject
public class ActiveObject
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
ActiveObject
s to work or allowing mouse and keyboard events to be processed.
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 |
---|
public ActiveObject()
ActiveObject
. The name of its thread in the debugger will be
"ActiveObject" followed by a unique id number.
public ActiveObject(Runnable target)
ActiveObject
that will run, not its own run()
method, but the run()
method of some other object that implements the
Runnable
interface.
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.public ActiveObject(Runnable target, String name)
ActiveObject
that will run, not its own run()
method, but the run()
method of some other object that implements the
Runnable
interface.
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.public ActiveObject(String name)
ActiveObject
whose thread will show up in the debugger with
the given name.
name
- Name of thread to be shown in debuggerMethod Detail |
---|
public void kill()
ActiveObject.pause()
or ActiveObject.yield()
.
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.
public static void killAll()
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 ActiveObject
s
from just merrily churning away with no applet left to draw on.
public static void pause(double millis)
pause(1000, 0)
would pause for one second.
millis
- number of milliseconds to pause forpublic static void pause(long millis)
pause(1000, 0)
would pause for one second.
millis
- number of milliseconds to pause forpublic static void pause(long millis, int nanos)
pause(1000, 0)
would pause for
one second, and pause(0, 500000)
would pause for half a millisecond.
millis
- number of milliseconds to pause fornanos
- number of additional nanoseconds to pause forpublic void run()
ActiveObject
will do when it is start()
ed.
run
in interface Runnable
run
in class Thread
public void start()
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.
start
in class Thread
public static void yield()