Skip to main content

Different States of a Thread:

Different states of a Thread: 

Threads are in a new state when they are created. In this state, the code has yet to be executed.

Runnable State: 

A Thread is moved to a Runnable State when it is ready to execute. In this state, a Thread might be running, or it might be prepared to run whenever there is a place to, organized by the Thread Scheduler.

In a Multi-Thread process, a specific amount of time is allocated to each Thread. Each Thread gets a specific allocated timeframe to run before switching to a new Thread, giving each equal opportunity to execute the actions. When this happens, all Threads running, waiting to run, or waiting for the CPU, lie in a Runnable State.

Blocked/Waiting state:

When a Thread is inactive for a brief time, it is said to be in one of these states:

  • Blocked
  • Waiting

The Thread Scheduler is responsible for reactivating and scheduling the blocked/waiting Thread back to a Runnable State. A Thread will not be able to continue until it has been moved back to the Runnable State. Whilst in a Blocked/Waiting state, there is no CPU cycle being consumed.


If a Thread is trying to access a part of code currently being blocked by another Thread, it is in the Blocked state. Only when the protected area is unlocked will the Scheduler pick the Thread and move it into the Runnable State. 


A Thread is in a Waiting state when there is a condition that is currently preventing the code from being run. Once the condition is fulfilled, the Thread will be sent to the Runnable state by the Thread Scheduler to continue.


If a Thread is moved to the Blocked/Waiting state, another Thread will be transferred to the Runnable state to run while the condition is fulfilled or granted access. This decision making is all controlled by the Thread Scheduler. 

Timed Waiting: 

A thread lies in a Timed Waiting state when a timeout parameter is called. A Thread will remain in this state until the timeout has finished or a notification has been received. For example, when a thread calls sleep, it is moved to the Timed Waiting state.

Terminated State: 

There are a few reasons that can lead to the Thread Terminating :

  • Because the Termination should exist. This happens when the program completes the code.
  • Because there was an error whilst running the code in the Thread.

A thread that lies in a terminated state does no longer consumes any cycles of CPU.

Popular posts from this blog

Executer vs ExecuterSevice: What's the Difference?

Executer and ExcuterService: Executor and ExecutorService are part of Java's Executor framework, which provides  thread pool  facilities to Java applications. Since creating and managing Threads are expensive, It's a better idea to create a thread pool that can execute in parallel rather than every time a new thread is requested, helping reduce the load. This improves the response time of the application. Differences between Executer and ExecuterService: One of the key differences between the Executor and ExecutorService interface is that the former is a parent interface while ExecutorService extends Executor.  A second significant difference between ExecutorService and Executor is that Executor defines an execute() method, which accepts the Runnable interface object. Simultaneously, a submit() method takes both Runnable and Callable object interfaces. Another difference between Executor and ExecutorService interface is that the execute() methods' return type is void, wh...

What happens if we don't override the run() method:

Consequences of not overriding the run() method: If we don’t override the run() method, the compiler will not flash any error, and it will execute the run() method of the Thread class that has been emptily implemented, So, there will be no output for this Thread. Below is an example of the method returning no output: public class Example extends Thread { public static void main(String[] args) { Thread thread = new Thread( new Example()); thread.start(); // will create a new thread and call thread class's run() method which has no implementation. } }

Creating and Running a Java Thread:

Ways to create a Thread: You can create a Thread in two ways: By extending the Thread class. By implementing a Runnable Interface. Thread class: The Thread class provides constructors and methods to create and perform operations on a thread. Runnable Interface: Runnable Interfaces have only one method,  named run() . The Runnable Interface should be performed by classes where their instances plan to be executed by a thread.  public void run() : is used to perform the action for a Thread. Starting a thread: The  start() method  of a Thread class is used to start a newly created thread. It performs the following tasks: A new thread starts(with their stack). The Thread moves to the Runnable state from New state. The  run() method  will run when the Thread gets a chance. Java Thread Example by extending Thread class: class  Multi  extends  Thread{   public   void  run(){   System.out.println( "Extended Thread Class E...