0 votes
in JAVA by
What are the states in the lifecycle of a Thread?

1 Answer

0 votes
by

A thread can have one of the following states during its lifetime:

  1. New: In this state, a Thread class object is created using a new operator, but the thread is not alive. Thread doesn't start until we call the start() method.
  2. Runnable: In this state, the thread is ready to run after calling the start() method. However, the thread is not yet selected by the thread scheduler.
  3. Running: In this state, the thread scheduler picks the thread from the ready state, and the thread is running.
  4. Waiting/Blocked: In this state, a thread is not running but still alive, or it is waiting for the other thread to finish.
  5. Dead/Terminated: A thread is in terminated or dead state when the run() method exits.

Java thread life cycle

A thread is a program in execution created to perform a specific task. Life cycle of a Java thread starts with its birth and ends on its death.

The start() method of the Thread class is used to initiate the execution of a thread and it goes into runnable state and the sleep() and wait() methods of the Thread class sends the thread into non runnable state.

After non runnable state, thread again comes into runnable state and starts its execution. The run() method of thread is very much important. After executing the run() method, the lifecycle of thread is completed.

All these phases of threads are the states of thread in Java.

To work with threads in a program, it is important to identify thread state. The following figure shows thread states in Java thread life cycle.

Thread States in Java

Thread States in Java

A thread is a path of execution in a program that goes through the following states of a thread. The five states are as follows:

  1. New
  2. Runnable
  3. Running
  4. Blocked (Non-runnable state)
  5. Dead

New (Newborn State)

When an instance of the Thread class is created a new thread is born and is known to be in New-born state. That is, when a thread is born, it enters into new state but its execution phase has not been started yet on the instance.

In simpler terms, Thread object is created but it cannot execute any program statement because it is not in an execution state of the thread. Only start() method can be called on a new thread; otherwise, an IllegalThreadStateException will be thrown.

Runnable State

The second phase of a new-born thread is the execution phase. When the start() method is called on a the new instance of a thread, it enters into a runnable state.
In the runnable state, thread is ready for execution and is waiting for availability of the processor (CPU time). There are many threads that are ready for execution, they all are waiting in a queue (line).

If all threads have equal priority, a time slot is assigned for each thread execution on the basis of first-come, first-serve manner by CPU. The process of allocating time to threads is known as time slicing. A thread can come into runnable state from running, waiting, or new states.

Running State

Running means Processor (CPU) has allocated time slot to thread for its execution. When thread scheduler selects a thread from the runnable state for execution, it goes into running state. Look at the above figure.

In running state, processor gives its time to the thread for execution and executes its run method. It is the state where thread performs its actual functions. A thread can come into running state only from runnable state.

A running thread may give up its control in any one of the following situations and can enter into the blocked state.

  1. When sleep() method is invoked on a thread to sleep for specified time period, the thread is out of queue during this time period. The thread again reenters into the runnable state as soon as this time period is elapsed.
  2. When a thread is suspended using suspend() method for some time in order to satisfy some conditions. A suspended thread can be revived by using resume() method.
  3. When wait() method is called on a thread to wait for some time. The thread in wait state can be run again using notify() or notifyAll() method.

Blocked State

A thread is considered to be in the blocked state when it is suspended, sleeping, or waiting for some time in order to satisfy some condition.

Dead State

A thread dies or moves into dead state automatically when its run() method completes the execution of statements. That is, a thread is terminated or dead when a thread comes out of run() method. A thread can also be dead when the stop() method is called.

During the life cycle of thread in Java, a thread moves from one state to another state in a variety of ways. This is because in multithreading environment, when multiple threads are executing, only one thread can use CPU at a time.

All other threads live in some other states, either waiting for their turn on CPU or waiting for satisfying some conditions. Therefore, a thread is always in any of the five states.

Thread Program Example:

ThreadDemo.java

  1. /* Thread 1 */  
  2. class Thread1 extends Thread   
  3. {  
  4.       
  5.     public void run()   
  6.     {  
  7.         System.out.println("Thread 1");  
  8.         System.out.println("i in Thread 1 ");  
  9.         for (int i = 1; i <= 5; i++)   
  10.         {  
  11.             System.out.println("i = " + i);  
  12.             try   
  13.             {  
  14.                 Thread.sleep(1000);  
  15.             }   
  16.             catch (InterruptedException e)   
  17.             {  
  18.                 e.printStackTrace();  
  19.             }  
  20.         }  
  21.         System.out.println("Thread 1 Completed.");  
  22.     }  
  23. }  
  24.   
  25. /* Thread 2 */  
  26. class Thread2 extends Thread   
  27. {  
  28.     public void run()   
  29.     {  
  30.         System.out.println("Thread 2");  
  31.         System.out.println("i in Thread 2 ");  
  32.         for (int i = 1; i <= 5; i++)  
  33.         {  
  34.             System.out.println("i = " + i);  
  35.         }  
  36.         System.out.println("Thread 2 Completed.");  
  37.     }  
  38. }  
  39.   
  40. /* Driver code */  
  41. public class ThreadDemo   
  42. {  
  43.     public static void main(String[] args) {  
  44.     // life cycle of Thread  
  45.     // Thread's New State  
  46.     Thread1 t1 = new Thread1();  
  47.     Thread2 t2 = new Thread2();  
  48.     // Both the above threads are in runnable state  
  49.     // Running state of Thread1 and Thread2  
  50.     t1.start();  
  51.     // Move control to another thread  
  52.     t2.yield();  
  53.     // Blocked State Thread1  
  54.     try   
  55.     {  
  56.         t1.sleep(1000);  
  57.     }   
  58.     catch (InterruptedException e)   
  59.     {  
  60.         e.printStackTrace();  
  61.     }  
  62.     t2.start();  
  63.     System.out.println("Main Thread End");  
  64.  }  
  65. }  

Output:

Thread 1
i in Thread 1 
i = 1
Main Thread End
Thread 2
i in Thread 2 
i = 1
i = 2
i = 3
i = 4
i = 5
Thread 2 Completed.
i = 2
i = 3
i = 4
i = 5
Thread 1 Completed.

Related questions

0 votes
asked Feb 9, 2021 in JAVA by SakshiSharma
0 votes
asked Apr 10, 2021 in JAVA by Robindeniel
...