Extending Thread
------------------------------------------------------
There are a couple ways of using threads. The first method we will examine
is to extend the Thread class. Here's a simple example (Keep in mind
that this is a command line application):
This application doesn't do much. It just counts from 1 to 11 slowly. You will
notice a few new items:
1. The class extends Thread.
2. The use of a sleep() method.
3. The use of a start() method.
4. The use of a run() method.
All these are parts of the thread class. Without these items the eleven lines
produced by this program would print out in a flash.
NOTE: You don't have to use any special imports for threads since the
Thread class is part of the java.lang package.
------------------------------------------------------
What Are Threads For? Threads allow for timing and continuous
activity. You can control the timing of movement of objects
in an applet with Threads. You can control the rate at which text appears
on the screen with Threads (as in the example above).
------------------------------------------------------
Threads In Applets: Since an applet class already extends Applet and
a class can only extend one superclass, if you want to extend a thread
and use it in an applet, the thread class must be a separate class from the
applet. Here's an example:
Here's the applet in action:
The point here is for you to examine the bounceThread class and notice that
like the above example it extends Thread and includes sleep(), run(), and
start() methods. [The start() method is actually located in the applet
class.]
One More Example: Combining the ideas presented in the
first two examples we get an applet which looks like this:
Here's the code:
Once again you will notice that the thread class extends Thread and there
are run(), start(), and sleep() methods.
------------------------------------------------------
Assignment: You will write an applet which displays a
growing and shrinking circle. You will have a growth cycle of seven
circles and a decay cycle of the same number of cycles. These cycles
will repeat for the lifetime of the applet (theoretically forever). If you
were animating an explosion you could do something very similar to this.