đź§µ Java Thread Lifecycle

VijayVijay
3 min read

In Java, a thread goes through a well-defined series of stages from the moment it is created to the moment it dies. This journey is known as the thread lifecycle.

State transition diagram for life cycle of thread in Java

1. NEW State

This is where it all begins. When you create a thread object, but haven’t started it yet.

✅ What’s happening?

  • Memory is allocated for the object.

  • But the thread hasn’t been handed over to the CPU yet.

At this point, the thread is in the NEW state.

2. RUNNABLE State

Once you call start(), the thread asks the CPU to be scheduled.

âś… What's happening?

  • Thread is ready to run, but not guaranteed to run immediately.

  • It is added to the OS scheduler's queue.

3. RUNNING State

When the thread is picked by the CPU from the queue, it enters the RUNNING state.

âś… What's happening?

  • Now, the CPU is actively executing your code in this thread.

  • The thread is alive and doing its work.

⚠️ Note: You cannot force a thread to be in this state—it is completely managed by the scheduler.

4. WAITING State

A thread moves to WAITING when it is waiting for another thread to tell it to continue.

âś… What's happening?

  • The thread voluntarily pauses and says, "I’ll wait here until someone wakes me up."

  • It will stay in this state indefinitely until another thread calls notify() or notifyAll().

5. TIMED_WAITING State

A thread goes into TIMED_WAITING when it’s told to pause for a specific amount of time.

âś… What's happening?

  • It’s like setting an alarm — “Wake me up in 2 seconds.”

  • Once the time is up, the thread goes back to RUNNABLE.

6. BLOCKED State

A thread becomes BLOCKED when it tries to enter a synchronized block that is already locked by another thread.

âś… What's happening?

  • It’s like waiting outside a locked door.

  • When the current holder exits, the waiting thread becomes RUNNABLE.

7. TERMINATED (a.k.a. DEAD) State

Once the thread finishes its task, or is stopped manually, it is no longer alive.

âś… What's happening?

  • The thread is now finished.

  • It can never be started again.

🔄 Summary of Transitions

StateTriggerCan Move To
NEWstart()RUNNABLE
RUNNABLECPU schedules itRUNNING
RUNNING-WAITING / TIMED_WAITING / BLOCKED / TERMINATED
WAITINGnotify() / notifyAll()RUNNABLE
TIMED_WAITINGTime expiresRUNNABLE
BLOCKEDLock is releasedRUNNABLE
TERMINATEDTask finishes / stopped— (end state)

Lifecycle Demonstration

Final Thoughts

  • Each thread moves through well-defined states — you can predict and control them with the right APIs.

  • Understanding the lifecycle helps avoid bugs like deadlocks, unnecessary blocking, or threads that never finish.

If you're serious about writing safe and performant concurrent Java code, understanding the thread lifecycle is non-negotiable.

0
Subscribe to my newsletter

Read articles from Vijay directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vijay
Vijay