Card 04/ 08
All 8 cards
ConceptDifficulty: Intermediate1 min
The States a Thread Moves Through, and Who Moves It
A thread dump on a stuck service shows forty threads and a word beside each one. Reading those words is most of diagnosing a hang, and there are only a handful of them.
| State | Means | Waiting for |
|---|---|---|
NEW | Created, never started | Nothing — nobody has called start() |
RUNNABLE | Able to run | A processor, or the operating system |
BLOCKED | Wants a lock somebody else holds | That lock |
WAITING | Asked to be woken later | Another thread, with no deadline |
TIMED_WAITING | The same, with a deadline | Another thread, or the clock |
TERMINATED | Its run method has returned | Nothing; it is finished |
The arrow that does not exist is the one from TERMINATED back to RUNNABLE. A thread that has finished is finished, and calling start() on it a second time throws IllegalThreadStateException.
So a Thread object is not a worker you hand jobs to. It is one job, run once, and the thing that lets you hand jobs to a reusable worker is a pool.