Java is not the first programming language to support
multi-threading. It is, though, the first to provide direct language
support for both threads and the graphical user interface tools (in the
Abstract Windowing Toolkit) that we have used to develop all of our lablets.
This combination allows us to write programs that are responsive to their
users in ways that are not easily achieved using other languages. In
essence, we can now write programs that are capable of responding to user
interface events while they are performing some other processing.
In the case
of this chapter's lablet,
TickTock
, we have what appears
to be a simple implementation of a modern digital alarm clock. What is
interesting about it from a programming standpoint is that the program
actually performs like a real clock. That is to say, while it is busy
displaying and updating the current time, it is still responsive to button
clicks which can interrupt this processing. When alternate processing,
like setting the alarm, is completed, the program returns to its regular
processing without a hitch. This, of course, is all made possible by
the fact that TickTock
uses a thread.
In the
exercises that follow we'll analyze and experiment with
TickTock
's
use of threads, and we'll extend it to accomplish more threaded behavior.
![]() |
In this lab, you will: | ||
![]() |
Run and experiment with the Chapter
11 lablet, |
||
![]() |
Analyze the lablet's use of threads and how they affect its performance. |
||
![]() |
Extend the lablet to implement another threaded mode of behavior, a chronometer. |
run()
method in a separate class, and have that class
either extend Thread
or implement the Runnable
interface. A different way is to have the applet itself implement the
Runnable
interface and have its own instance of a
Thread
and its own run()
method, as we did in
version 3 of the bouncing ball
applet [pp. 470-471 ].TickTock
so that the applet has its own thread, and comment
on whether you think the resulting code would be easier or harder
for a programmer to read and modify.
TickTock
so that it keeps correct
Jovian time.
Sortmeister
even more
revealing about comparative sorting algorithms. Rewrite Sortmeister
so that two sort algorithms can be applied to the same data and we can
compare their performance side-by-side. That is, have your new program create
two "sorting displays," each executing a different algorithm on the same
original data.