What are all the Stages of Threads in Java ?

Java provides five distinct thread states, each representing the various stages a thread can go through during its lifecycle. These states are part of the Thread.State enum, which is used to represent the state of a thread at any point in time.

1. NEW

  • Description: A thread that has been created but not yet started. In this state, the thread has been instantiated but its start() method has not been called yet.
  • Behavior: The thread remains in the NEW state until the start() method is invoked.
  • Transition: When start() is called, the thread moves to the RUNNABLE state.

Example:

Thread t = new Thread(() -> System.out.println("Running")); // Thread is in NEW state now.

2. RUNNABLE

  • Description: A thread that is ready to run and may be executing code. In this state, the thread is either executing on the CPU or waiting for CPU time to be allocated.
  • Behavior: The thread can move in and out of this state as it gets CPU time or gets interrupted by the scheduler.
  • Transition: The thread enters the RUNNABLE state when start() is called, and it may stay in this state until it completes its execution, moves to BLOCKED, WAITING, or TIMED_WAITING.

Example:

t.start(); // The thread is now in the RUNNABLE state

3. BLOCKED

  • Description: A thread that is trying to acquire a lock but is blocked because another thread is holding that lock.
  • Behavior: The thread enters this state when it attempts to enter a synchronized block or method but another thread already holds the intrinsic lock for the object.
  • Transition: The thread transitions to BLOCKED when it tries to access a synchronized resource held by another thread. It moves back to RUNNABLE once the lock is released.

Example:

synchronized(lock) { // Thread that acquires the lock first can enter. // Other threads will be BLOCKED until the lock is released. }

4. WAITING

  • Description: A thread that is waiting indefinitely for another thread to perform a particular action. This can occur when a thread calls Object.wait() or Thread.join() without a timeout.
  • Behavior: The thread waits until another thread signals it via notify() or notifyAll(), or until the thread it’s waiting on finishes.
  • Transition: The thread enters WAITING when calling methods like Object.wait(), Thread.join(), or LockSupport.park(). It moves back to RUNNABLE when it is notified or when the thread it’s waiting for completes.

Example:

synchronized(lock) { lock.wait(); // Thread will enter WAITING state }

5. TIMED_WAITING

  • Description: A thread that is waiting for another thread to perform an action for a specified amount of time. This state is similar to WAITING, but with a time limit after which the thread automatically moves to RUNNABLE.
  • Behavior: The thread remains in TIMED_WAITING until the specified waiting period elapses, or if another thread interrupts it or performs the expected action (e.g., notify()).
  • Transition: The thread enters TIMED_WAITING when it calls methods like Thread.sleep(), Object.wait(long), Thread.join(long), or LockSupport.parkNanos(). It moves back to RUNNABLE after the timeout expires or the waiting condition is met.

Example:

Thread.sleep(1000); // Thread will enter TIMED_WAITING state for 1 second

6. TERMINATED

  • Description: A thread that has completed its execution. It may have finished either by completing its run() method or by throwing an uncaught exception.
  • Behavior: Once a thread reaches the TERMINATED state, it cannot be restarted.
  • Transition: The thread enters TERMINATED after the run() method completes or if an exception causes the thread to stop.

Example:

t.join(); // Thread will enter TERMINATED state after completion

Thread Lifecycle Transitions

The transitions between these states can be summarized as:

  1. NEWstart()RUNNABLE
  2. RUNNABLEBLOCKED (if waiting for a lock)
  3. RUNNABLEWAITING/TIMED_WAITING (waiting for an event or sleeping)
  4. BLOCKED/WAITING/TIMED_WAITINGRUNNABLE (after receiving a lock or notification)
  5. RUNNABLETERMINATED (after completion of the thread’s task)

Thread State Diagram

Here is a conceptual state diagram that visualizes the lifecycle of a thread:

NEW -------------------> RUNNABLE ------------------> TERMINATED
        start()                 CPU time complete or     (After thread run finishes)
                                encounter issues  

            |                         |
            |                         |
         BLOCKED ------------------- WAITING/TIMED_WAITING
  (Waiting for a lock)        (Waiting for condition/timeout)

Conclusion

Understanding Java thread states helps you to manage thread behavior in a multithreaded environment. This is crucial for diagnosing and resolving issues related to performance, concurrency, and deadlocks in Java applications.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *