Using the Threads in Applets


Prerequisites

This Magercise shows how to control threads in an Applet. Since an applet may not be active for all of its lifetime, such as when a user browses to a different page, its threads may need to be suspended while the applet is not active. When the applet is finally disposed (usually due to removal from the brower's cache), all of an applets suspended threads should be stopped so they can be reclaimed by Java's garbage collector.

Here a Counter object is used to show a thread's activity. The thread for the Counter will be suspended when the applet is made inactive (when its stop method is called), resumed when the applet is made active again (when start is called on the applet), and stopped when the applet is destroyed.

Methods for controlling threads are covered in the course notes section Controlling Thread Execution.

Work Location

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

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. Add a start method to the applet that will resume the Counter.

  2. Add a stop method to the applet that will suspend the Counter.

  3. Add a destroy method to the applet that will stop the Counter's 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.