Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheClock
applet shown below displays the current time and updates its display every second. You can scroll this page and perform other tasks while the clock continues to update because the code that updates the clock's display runs within its own thread.
Note: Because some old browsers don't support 1.1, the above applet is a 1.0 version (here is the 1.0 code; here's the 1.1 code). To run the 1.1 version of the applet, go toexample-1dot1/Clock.html
. For more information about running applets, refer to About Our Examples.The
Clock
applet uses a different technique thanSimpleThread
for providing therun
method for its thread. Instead of subclassingThread
,Clock
implements theRunnable
interface (and therefore implements the run method defined in it).Clock
then creates a thread and provides itself as an argument to theThread
's constructor. When created in this way, theThread
gets itsrun
method from the object passed into the constructor. The code that accomplishes this is shown in bold here:Theimport java.awt.Graphics; import java.util.*; import java.text.DateFormat; import java.applet.Applet; public class Clock extends Applet implements Runnable { private Thread clockThread = null; public void start() { if (clockThread == null) { clockThread = new Thread(this, "Clock"); clockThread.start(); } } public void run() { Thread myThread = Thread.currentThread(); while (clockThread == myThread) { repaint(); try { Thread.sleep(1000); } catch (InterruptedException e){ // the VM doesn't want us to sleep anymore, // so get back to work } } } public void paint(Graphics g) { // get the time and convert it to a date Calendar cal = Calendar.getInstance(); Date date = cal.getTime(); // format it and display it DateFormat dateFormatter = DateFormat.getTimeInstance(); g.drawString(dateFormatter.format(date), 5, 10); } // overrides Applet's stop method, not Thread's public void stop() { clockThread = null; } }Clock
applet'srun
method loops until the browser asks it to stop. During each iteration of the loop, the clock repaints its display. Thepaint
method figures out what time it is, formats it in a localized way, and displays it. You'll see more of theClock
applet in The Life Cycle of a Thread which uses it to teach you about the life of a thread.
Runnable
InterfaceYou have now seen two ways to provide the run method for a Java thread:There are good reasons for choosing either of these options over the other. However, for most cases, including that of the
- Subclass the
Thread
class defined in thejava.lang
package and override therun
method.
Example: See theSimpleThread
class described in SubclassingThread
and Overridingrun
.- Provide a class that implements the
Runnable
interface (also defined in thejava.lang
package) and therefore implements therun
method. In this case, aRunnable
object provides therun
method to the thread.
Example: See theClock
applet just shown.Clock
applet, the following rule of thumb will guide you to the best option.
Rule of Thumb: If your class must subclass some other class (the most common example beingApplet
), you should useRunnable
as described in option #2.To run in a Java-enabled browser, the
Clock
class has to be a subclass of theApplet
class. Also, theClock
applet needs a thread so that it can continuously update its display without taking over the process in which it is running. (Some browsers might create a new thread for each applet so as to prevent a misbehaved applet from taking over the main browser thread. However, you should not count on this when writing your applets; your applets should create their own threads when doing computer-intensive work.) But since the Java language does not support multiple class inheritance, theClock
class cannot be a subclass of bothThread
andApplet
. Thus theClock
class must use theRunnable
interface to provide its threaded behavior.
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2002 Sun Microsystems, Inc. All rights reserved.