Creating a Threaded Slide Show Applet
A thread is a path of execution through a program. Single threaded programs have one path of
execution, and multithreaded programs have two or more paths of execution. Single threaded
programs can perform only one task at a time, and have to finish each task in sequence before
they can start another. For most programs, one thread of execution is all you need, but
sometimes it makes sense to use multiple threads in a program to accomplish multiple
simultaneous tasks.
For example, a multithreaded math program can let the user set parameters for a new
calculation while a previous calculation computes. Or a multithreaded word processor can let
the user open a new document while a large file saves or spools to the printer. And
sometimes, as you will see in this article, a multithreaded approach is necessary when you
need a method to execute in a continuous loop in one thread and another thread to do
something else like paint the display between loop iterations.
This article describes multithreaded programming by presenting an example slide show applet.
The images for the slide show applet are thumbnails of paintings by Claude Monet downloaded
from a public web site. If Claude Monet were alive today, the thumbnails and slide show
applet might very well be available from his own web site so prospective buyers can view his
latest works and make electronic purchases.
All single or multithreaded programs execute in their own thread created and started by the
underlying Java VM. The Java VM also creates and starts other threads to help manage single
or multithreaded program execution. For example, the Java VM starts the Finalizer thread to
execute an object's finalize method before the object is garbage collected, and starts the
AWT-EventQueue-0 thread to call an object's event handling methods such as actionPerformed
and windowClosing. Because the Java VM spawns threads to handle the execution of a program,
you might say that the Java VM itself is a multithreaded program.