The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Essential Java Classes
Lesson: Threads: Doing Two or More Tasks At Once

Implementing the Runnable Interface

The Clock applet 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.

The Clock applet uses a technique different from SimpleThreadís for providing the run method for its thread. Instead of subclassing Thread, Clock implements the Runnable interface and therefore implements the run method defined in it. Clock then creates a thread with itself as the Threadís target. When created in this way, the Thread gets its run method from its target. The code that accomplishes this is highlighted:

import 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");
    public void run() {
        Thread myThread = Thread.currentThread();
        while (clockThread == myThread) {
            try {
            } 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 =
        g.drawString(dateFormatter.format(date), 5, 10);
    //overrides Appletís stop method, not Threadís
    public void stop() {
        clockThread = null;
The Clock appletís run method loops until the browser asks it to stop. During each iteration of the loop, the clock repaints its display. The paint method figures out what time it is, formats it in a localized way, and displays it. Youíll see more of the Clock applet in the section The Life Cycle of a Thread (in the Essential Java Classes trail), which uses it to teach you about the life of a thread.

Deciding to Use the Runnable Interface

You have now seen two ways to provide the run method. There are good reasons for choosing either of these options over the other. However, for most cases, including that of the Clock applet, if your class must subclass some other class (the most common example being Applet), you should use Runnable.

To run in a browser, the Clock class has to be a subclass of the Applet class. Also, the Clock 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 because the Java programming language does not support multiple-class inheritance, the Clock class cannot be a subclass of both Thread and Applet. Thus, the Clock class must use the Runnable interface to provide its threaded behavior.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.