Review how threads enter the waiting and dead states.
Waiting and dying Threads
Waiting State of a Thread
There are several ways in which a thread moves from the Running state to the Waiting state:
The sleep() method: The sleep() method takes a time argument. When a thread invokes its sleep() method, the thread enters the waiting state until this time has expired.
Blocking on I/O: When a thread performs an I/O operation, it enters the waiting state until the I/O operation is completed. This occurs when a thread is waiting for an I/O operation to be completed.
Waiting for a lock: When a thread executes a synchronized method or statement, it waits until it acquires a lock on the object or class which is being synchronized.
Waiting to be notified: When a thread invokes the wait() method of an object, it must wait until it is notified that it can leave the waiting state.
In each of the above cases, when a thread leaves the waiting state, it enters the Runnable (ready) state. If a thread is waiting and another thread invokes its interrupt() method, then the waiting thread moves to the Runnable (ready) state.
When the waiting thread reenters the running state, the InterruptedException is thrown.
Dead State of a Thread
A thread enters the dead state when it returns from its run() method.
A thread is done being a thread when its target run() method completes. When a thread completes its run() method, the thread ceases to be a thread of execution.
The stack for that thread dissolves, and the thread is considered dead. (Technically, the API calls a dead thread "terminated," but I will use "dead" in this module.) It is still a Thread object, just not a thread of execution.
Therefore, if you have a reference to a Thread instance, then even when that Thread instance is no longer a thread of execution, you can still call methods on the Thread instance, just like any other Java object.
However, you will not be able to call start() again.
When a thread enters the dead state, it may not be restarted. However, other objects may still access its properties and methods.
Thread Creation - Quiz
Click the Quiz link below to check your understanding of multithreading and thread creation. Thread Creation - Quiz