This example will repeat any code above the sleep code every second, as specified by the
1000
millisecond parameter to the
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.
The
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
try
-
catch
exception[1] handling code in the next module.
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.