Java Reference
In-Depth Information
It is often useful to know when a thread has ended. For example, in the preceding examples,
for the sake of illustration it was helpful to keep the main thread alive until the other threads
ended. In those examples, this was accomplished by having the main thread sleep longer
than the child threads that it spawned. This is, of course, hardly a satisfactory or generaliz-
able solution!
Fortunately,
Thread
provides two means by which you can determine if a thread has
ended. First, you can call
isAlive( )
on the thread. Its general form is shown here:
final boolean isAlive( )
The
isAlive( )
method returns
true
if the thread upon which it is called is still running. It
returns
false
otherwise. To try
isAlive( )
, substitute this version of
MoreThreads
for the
one shown in the preceding program: