Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Life Cycle of a Thread
3.
Stages of Life Cycle
4.
Implementation of Thread States
5.
Different Thread methods
6.
Examples 
6.1.
Code 1
6.2.
Code 2
7.
Frequently Asked Questions
8.
Key Takeaways
Last Updated: Mar 27, 2024

Life Cycle Of A Thread

Crack Google SDE interview : Essential projects
Speaker
Saurav Prateek
SDE-2 @
20 Jun, 2024 @ 01:30 PM

Introduction

In this article, we will be learning about the life cycle of a thread, but before jumping on to the main concept, we shall take a look at what is a thread. A thread can be defined as the path or direction taken while executing the code. All programs have  At least one Thread, known as the main Thread, provided by the JVM. The thread allows the program to perform more efficiently by performing multiple tasks simultaneously.

Must Read, Multithreading in java, Duck Number in Java and Hashcode Method in Java

Life Cycle of a Thread

The life cycle of a thread can be defined as the state transitions of a thread that starts from its birth till it ends. There are several life cycles in which a thread lies at any instant.

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Stages of Life Cycle

  • New: When we create a new Thread object using the Thread class, a new thread starts its life cycle in the new state. The Thread which is born is known as Newborn State. This remains in this state until the program begins the Thread. With the help of the start() method, you can call the thread, otherwise, it might show some error.
     
  • Runnable: As soon as the start() method is called, the thread went from the New state into a runnable state. In this state, a thread has joined the queue of threads that are waiting for its execution.
     
  • Waiting: When a thread is in waiting mode, it waits for another thread to perform its task.
     
  • Timed Waiting: A runnable thread can transfer the timed waiting state for another to execute its task. 
     
  • Terminated(Dead): A thread moves into a dead state when the run() method completes its execution. A thread method can also be finished when the stop() method is called.
     
  • Blocked: When a thread is considered as blocked which means that it is suspended, sleeping, or waiting for little time in order to validate some conditions.

Implementation of Thread States

This represents the initial state of the Thread that is the NEW state

public static void final Thread.State NEW


This represents the Thread's runnable state, which means that the Thread is waiting in the queue to run.

public static void final Thread.State RUNNABLE


This represents the waiting state, which means waiting for another thread to complete its task.

public static void final Thread.State WAITING


This represents the timed waiting state. The critical difference between waiting and timed waiting is the time constraint.

public static void final Thread.State TIMED_WAITING


This represents the final state of a thread that is terminated. This means that it has completed its execution.

public static void final Thread.State TERMINATED


This represents the blocked state, which means that the threads are waiting for their chance to get locked.

public static void final Thread.State BLOCKED

Different Thread methods

Methods

 

Description

 

start()

 

This method will begin the Thread execution, and JVM calls the run() method on the Thread.
Sleep(int milliseconds) This method makes the thread sleep; hence the execution of the Thread will pause for milliseconds.

getName()

 

This function will return the name of the thread.
setPriority(int newpriority) This method will change the Thread's priority.
yield() This causes the current Thread and other threads to execute.

Examples 

Code 1

public class Ex28 extends Thread {
	public static void main(String[] args) {
		Ex28 thread = new Ex28();
		thread.start();
		System.out.println("This part of the code is running outside the thread");
	}
	public void run() {
		System.out.println("This part of the code is running inside a thread");
	}
}

Output

This part of the code is running outside the thread
This part of the code is running inside a thread


In this code, we have taken the simple example of Thread, and we have shown its execution. 

Practice by yourself on java online compiler.

Code 2

public class Ex29 implements Runnable 
{
	public static void main(String[] args) 
	{
		Ex29 obj = new Ex29();
		Thread thread = new Thread(obj);
		thread.start();
		System.out.println("This part of code is outside of the thread");
	}
	public void run() {
		System.out.println("This part of code is running in a thread");
	}
}

Output

This code is outside of the Thread
This code is running in a thread.

In this example, we have used the Thread's state, which is runnable, and we have clearly shown its execution.

Frequently Asked Questions

  1. What are the states of the thread life cycle in Java/
    A thread life cycle can be found in the given following five states. It can transfer from one state to another state. In Java, the life cycle of a thread has five states: New, Runnable, Waiting, Timed Waiting, Terminated.
     
  2.  Is Thread alive in Java?
    The Thread is not alive in Java. When we call the start() method on the Thread object, it changes to Runnable. Thus the control is transferred to the thread scheduler to finish its execution.

Key Takeaways

In this blog, we have covered a topic on the life cycle of a thread. We gave a brief introduction to the life cycle of a thread and the different thread stages. We also discussed the implementation of different thread stages. We also discussed different types of thread methods. We also took a few examples explaining different thread stages.

If you want to know more about these topics, please visit our website, Guided Paths and solve more interview problems Problems.

Recommended Readings:

 

Previous article
Multithreading in Java
Next article
Creating a Thread
Live masterclass