Java Reference
In-Depth Information
is in milliseconds. The thread then dies - i.e. it cannot be restarted - once the
process exits from
run()
.
/** Demo threading with Runnable implementation.**/
public class
MyApplet
extends java.applet.Applet
{
/** Applet's start method creates and starts a thread.
**/
public void start ()
{
// Create an instance of MyThread.
MyThread myThread = new MyThread ();
// Start the thread
myThread.start ();
} // start
public void paint(java.awt.Graphics g) {
g.drawString("Thread Demo 1",20,20);
}
} // class MyApplet
The diagram in Figure 8.1 shows schematically how the main thread and
MyThread
thread run in parallel.
Figure 8.1
This diagram illustrates threading with a
Thread
subclass.
MyApplet
creates an instance of
MyThread
, which extends
Thread
and overrides
run()
. The
applet invokes the
start()
method for the
MyThread
object. The
start()
method
returns but the thread process continues independently with the invocation of the
run()
method in
MyThread
. When the thread process returns from
run()
the thread
dies (i.e. cannot be started again).
Search WWH ::
Custom Search