This example will repeat any code above the sleep code every second, as specified by the
1000 millisecond parameter to
sleep() method. You could increment an image counter and repaint an applet window in this code to establish an
animation that updates its image every second.
currentThread() method is called to ensure that the
while loop only executes while the current thread is active.
In this case there is only one thread so the
while loop is effectively an infinite loop.
You will learn about the
catch exception handling code in the next module.
Exception: An exceptional situation that a program does not know how to handle is known as an exception.
An example of an exception is attempting to divide a number by zero or the system running out of memory.
In the next lesson, you will examine thread synchronization and how it impacts multithreaded programming.
Click the Exercise link below to automate the SlideShow applet.
Modify Slide Show - Exercise