Nearby lessons

68 of 125

Java - Thread Life Cycle

📌 What You Will Learn
  • What a thread is and how it is different from a process
  • Two ways to create a thread: Thread class and Runnable interface
  • The thread life cycle (states)
  • How threads cooperate: synchronized, wait and notify
  • The modern way: Executors, Callable and lambdas

Thread Life Cycle is a core concept of the Java language. This lesson explains Thread Life Cycle — The States and Thread States — The Full Life Cycle with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Thread Life Cycle — The States

A thread travels through states during its life:

StateMeaning
NEWThread object created, start() not yet called
RUNNABLEstart() called; thread is ready to run or running
BLOCKEDWaiting for a lock so it can enter a synchronized block
WAITINGWaiting for another thread to notify it
TIMED_WAITINGWaiting for a fixed time (sleep, join)
TERMINATEDrun() finished; thread is dead
Example01
JCode Cell
1Thread t = new Thread(task); // NEW
2t.start(); // RUNNABLE
3Thread.sleep(100); // TIMED_WAITING
4// after run() ends: // TERMINATED

Thread States — The Full Life Cycle

The classic material lists the thread states like this (modern Java names them slightly differently, but the ideas are the same):

StateMeaning
NEWThread object created; start() not yet called
RUNNABLEReady to run or currently running
BLOCKED / WAITINGWaiting for a lock, or waiting to be notified
TIMED_WAITINGWaiting for a fixed time (sleep/join with timeout)
TERMINATEDrun() finished; the thread is dead
Example02
JCode Cell
1NEW -> start() -> RUNNABLE -> run() ends -> DEAD
2 |
3 +--> (blocked) waiting for lock, sleep, or join
4 | |
5 +------------+ (unblocked -> back to RUNNABLE)
📝 Key Takeaways
  • A thread is a small unit of work; a process is a running program.
  • Create a thread by extending Thread or (better) implementing Runnable.
  • Life cycle: NEW -> RUNNABLE -> BLOCKED/WAITING/TIMED_WAITING -> TERMINATED.
  • Shared data races when many threads change it; synchronized gives a lock.
  • wait/notify let threads talk; they need the lock.
  • Deadlock = threads waiting on each other's locks; avoid by consistent lock order.
  • Real projects use Executors + Callable instead of raw threads.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1