|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
TheClockapplet displays the current time and updates its display every second. You can scroll the page and perform other tasks while the clock updates. The reason is that the code that updates the clock's display runs within its own thread.
Note: If you don't see the applet running above, you need to install Java Plug-in, which happens automatically when you install the J2SE JRE or SDK. This applet requires version 5.0 or later. You can find more information in the Java Plug-in home page.The
Clockapplet uses a technique different fromSimpleThread's for providing therunmethod for its thread. Instead of subclassingThread,Clockimplements theRunnableinterface and therefore implements therunmethod defined in it.Clockthen creates a thread with itself as theThread's target. When created in this way, theThreadgets its run method from its target. The code that accomplishes this is highlighted:Theimport java.awt.*; import java.util.*; import java.applet.*; import java.text.*; public class Clock extends java.applet.Applet implements Runnable { private volatile Thread clockThread = null; DateFormat formatter; // Formats the date displayed String lastdate; // String to hold date displayed Date currentDate; // Used to get date to display Color numberColor; // Color of numbers Font clockFaceFont; Locale locale; public void init() { setBackground(Color.white); numberColor = Color.red; locale = Locale.getDefault(); formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale); currentDate = new Date(); lastdate = formatter.format(currentDate); clockFaceFont = new Font("Sans-Serif", Font.PLAIN, 14); resize(275,25); } 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){ } } } public void paint(Graphics g) { String today; currentDate = new Date(); formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale); today = formatter.format(currentDate); g.setFont(clockFaceFont); // Erase and redraw g.setColor(getBackground()); g.drawString(lastdate, 0, 12); g.setColor(numberColor); g.drawString(today, 0, 12); lastdate = today; currentDate=null; } public void stop() { clockThread = null; } }Clockapplet'srunmethod loops until the browser asks it to stop. During each iteration of the loop, the clock repaints its display. Thepaintmethod figures out what time it is, formats it in a localized way, and displays it. You'll see more of theClockapplet in the section The Life Cycle of a Thread, which uses it to teach you about the life of a thread.
You have now seen two ways to provide therunmethod.There are good reasons for choosing either of these options over the other. However, for most cases, including that of the
- Subclass the
Threadclass and override therunmethod. See theSimpleThreadclass described in the section Subclassing Thread and Overriding run.
- Provide a class that implements the
Runnableinterface and therefore implements therunmethod. In this case, aRunnableobject provides therunmethod to the thread. See theClockapplet in the preceding section.Clockapplet, if your class must subclass some other class (the most common example beingApplet), you should useRunnable.To run in a browser, the
Clockclass has to be a subclass of theAppletclass. Also, theClockapplet 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 because the Java programming language does not support multiple-class inheritance, theClockclass cannot be a subclass of bothThreadandApplet. Thus, theClockclass must use theRunnableinterface to provide its threaded behavior.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.