×

Thread States in Java

An Item's Life Cycle (Thread States)

A thread can be in any of the states mentioned above at any given time in Java. They are as follows:

  1. New
  2. Active
  3. I'm blocked or waiting
  4. Temporary Waiting
  5. Terminated

Justification of various Threads States

Threads are always in a new state when they are first formed. The code has not yet been executed and, as a result, has not started for a thread in the new state.

When a thread calls the start() method, it transitions from the new state to the active state. Running and runnable are two of the two states that make up the active state.

  1. Runnable: After a thread has reached the runnable state, it is prepared to begin execution. The thread is currently in a runnable condition.The software allows a set amount of time for each unique thread when using multithreading. Every thread executes for a brief period, after which it voluntarily transfers control of the CPU to another thread so that the latter line may complete its portion of the time allotted for execution. Whenever such an event occurs, all threads that are ready to run and awaiting their chance to do so are in the runnable state. The threads are stored in a queue while they are in the runnable state.
  2. Running: The thread transitions from the runnable state to the running state when it receives the CPU. The most frequent transformation of a thread's state is from runnable to running.Blocked or Waiting: Whenever a thread is inactive for an extended period (but not permanently), it is either blocked or waiting.

    As an illustration, a thread (let's call it A) might desire to print some data to the printer. But concurrently, the other line (let's call it B) prints some data on the printer. Since thread B must use the printer first, thread A must wait. As a result, thread A is blocked. Because it cannot execute anything, a thread in the blocked state never uses a Central Processing Unit cycle (CPU). Therefore, we may argue that thread A is still inactive.Until thread A, which is in the waiting or blocked state, is reactivated by the thread scheduler.

    The main thread is considered waiting when it calls the join() method. After that, the main thread waits for the subordinate threads to finish their jobs. A notification is issued to the main line when the child threads finish their work, which switches the thread from waiting to the active state.

    The task of the thread scheduler is to choose which threads to accept and which to refuse when there are many threads in the waiting or blocked state. The selected thread is then given a chance to run.
  3. Timed Waiting: Occasionally, awaiting leadsto hunger. For instance, a thread (whose name is A) has entered a code's vital area and is unable to leave it. In this case, another thread (whose name is B) is forced to wait forever, which causes it to starve. A timed waiting state is provided to thread B in order to prevent this occurrence. Therefore, thread is only in the waiting state for a specific time, not permanently. When we use the sleep() method on a particular thread, we are actually engaging in timed waiting. The thread enters the timed wait state using the sleep() function. The thread awakens when the allotted time has passed and resumes execution where it left off.
  4. Terminated: A thread enters the terminating state for a reason stated below:
  • A thread exists or generally ends once it has completed its task.
  • A segmentation issue or unintended exception are examples of uncommon events that can cause an abnormal termination.
  • A thread that has been ended means it is no longer in the system. In other words, the thread is inactive and cannot be revived (made active again after being killed).

Related Topics

Series Program in Java

Series Program in Java The series program in Java is written to print the mathematical series such as the Fibonacci series, Pell series, etc. A few of the renowned series are...

12 minutes read.

Thread Synchronization in Java

In Java, the smallest processing component is a thread, which is a small subprocess. It follows a different course of action. Threads are autonomous. If an exception occurs in one thread,...

6 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Math fma () method in java

In java, the Math module constitutes of fma () Method in it. This method can be accessed in two ways which can be differentiated by the parameters which are given...

4 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Centered Square Numbers in Java

In this tutorial, we will understand how to check the number is a centered square number. It is one of the prevalent interview questions of IT companies. Firstly, we will...

3 minutes read.

Prepared statement in Java

Prepared statement: A prepared statement is a statement that is pre-compiled SQL statement, and it is a sub-interface of a statement. Compared to other statement objects in java, Prepared Statement objects have...

3 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Java String Matches vs Contains

String Matches in Java The matches() function and its variations are used to determine whether or not a provided text matches a regular expression. The functioning as well as output of...

3 minutes read.

Java vs Dot Net

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Difference between Abstract Class and Interface

There is a similarity between abstract class and interface is that we cannot create objects for both of them. But irrespective of this, there are some differences between them, let’s...

2 minutes read.

Davis Staircase Problem in Java

Davis has several stairs in his home and prefers to ascend one, two, or three steps at a time. As a highly clever youngster, he thinks about how many ways...

3 minutes read.

Java Boolean toValue() Method

The valueOf() method of Java Boolean class returns a Boolean object representing the given Boolean or String value. It returns true, if the specified Boolean or string object is true...

2 minutes read.

Bucket Sort in Java

Bucket Sort in JavaBucket sort is also called bin sort. Bucket sort first puts the elements of the array or list into different buckets. The first bucket contains elements of the smallest value. The...

11 minutes read.