Card 04/ 08

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.

A thread moves from new to terminated and never goes back
What each state means in a thread dump
StateMeansWaiting for
NEWCreated, never startedNothing — nobody has called start()
RUNNABLEAble to runA processor, or the operating system
BLOCKEDWants a lock somebody else holdsThat lock
WAITINGAsked to be woken laterAnother thread, with no deadline
TIMED_WAITINGThe same, with a deadlineAnother thread, or the clock
TERMINATEDIts run method has returnedNothing; 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.