Summary: Two ways of Thread Creation/ Two of Death
Creating Threads
- Extending the class Thread as was done in simple threads example.
- Implementing the interface Runnable and then declaring threads with
objects from the class that implements Runnable passed passed to the
Thread constructor as was done in the clock example.
Thread Deaths
- A natural death via a normal exit of the thread's run method as was done
in the simple thread example.
- A premature death by being killed (stopped).
Warning: Unfortunate Deaths
A thread can be killed at any time by calling it stop method. For
example,
p1.stop();
The stop method causes a sudden termination of the thread's run method.
If the run method performs critical or sensitive calculations, stop may
leave the program in an inconsistent or awkward state. Normally, you
should not use a thread's stop method but arrange for a gentler
termination by setting a condition that causes the run method to exit
as was done in the previous examples.