Thread Priority and Scheduling


Prerequisites

Each system may implement Java threads in a different manner. In particular, some systems use preemptive multitasking, while others may use cooperative implementation. Judicious use of yield can help avoid liveness problems on systems that do not have a preemptive thread implementation. Thread switches may also occur when blocking on i/o calls, such as when writing to standard out, or even to an AWT text widget.

In this Magercise thread activity is tracked using a ThreadGraph class that records the identity of threads that call its ping method, and then draws a history graph of that activity. The PingThread class is used to call the ping method of the ThreadGraph. The run method of the PingThread class will be modified to demonstrate thread scheduling behavior on your machine using sleep and yield. The effects of thread priority will also be explored.

Aspects of thread scheduling are covered in the course notes section Thread Liveness.

Work Location

Perform all work for this magercise in VisualAge project MageLang Magercises, package magercises.Thread Priority and Scheduling.

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. Run the unmodified applet a few times to show runtime behavior of your system's thread implementation.

  2. Modify the run method of PingThread to have it sleep for a random amount between pings. Examine runtime behavior by running the applet several times.

  3. Remove the sleep call, and instead have each PingThread yield after each ping. Examine runtime behavior by running the applet a few times.

  4. Modify the priority of one thread to be higher than the others. Examine its runtime behavior with and without a random sleep in it's run method. Also examine with a yield in the run method.

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.