Using the Runnable Interface


This Magercise shows how to use the Runnable interface to add liveness to an object.

Here a Clock class is given. It is implemented by subclassing Thread. The goal is to create a clock that is an AWT component and achieves liveness by implementing the Runnable interface and creating its own thread. The ClockPanel class is given as a starting point for the new clock, it includes almost all the AWT code needed for the new clock.

By using the Runnable interface instead of inheriting from the Thread class, the ClockPanel is free to inherit from the AWT class Panel, and can hide control of its thread from other objects.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.Using the Runnable Interface.

If this project does not appear in your Workspace add it from the repository (if it exists there) or create a new project using this name.

Tasks

Perform the following tasks:

  1. Modify the ClockPanel class to be Runnable, and add an appropriate run method.

  2. Have the ClockPanel create its own thread.

  3. Modify the run method to reference the new thread.

The task numbers above are linked to the step-by-step help page. Also available is a complete solution to the problem, and expected behavior, to demonstrate it.

Copyright © 1996-1997 MageLang Institute. All Rights Reserved.