The Thread Classes

Java has two ways a program can implement threading. One is to create a subclass of java.lang.Thread. However sometimes you'll want to thread an object that's already a subclass of another class. Then you use the java.lang.Runnable interface.

The Thread class has three primary methods that are used to control a thread:

public native synchronized void start()
public void run()
public final void stop()
The start() method prepares a thread to be run; the run() method actually performs the work of the thread; and the stop() method halts the thread. The thread dies when the the run() method terminates or when the thread's stop() method is invoked.

You never call run() explicitly. It is called automatically by the runtime as necessary once you've called start(). There are also methods to supend and resume threads, to put threads to sleep and wake them up, to yield control to other threads, and many more. I'll discuss these later.

The Runnable interface allows you to add threading to a class which, for one reason or another, cannot conveniently extend Thread. It declares a single method, run():

public abstract void run()
By passing an object which implements Runnable to a Thread() constructor, you can substitute the Runnable's run() method for the Thread's own run() method. (More properly the Thread object's run() method simply calls the Runnable's run() method.)


Previous | Next | Top
Last Modified January 26, 1998
Copyright 1997-8 Elliotte Rusty Harold
elharo@metalab.unc.edu