States of Thread in Java

“It is far easier to design a class to be thread-safe than to retrofit it for thread safety later.” ― Brian Goetz

Ravidu Perera
Level Up Coding
Published in
4 min readFeb 11, 2021

--

Thread states by codeGym

In concurrent programming, Java threads play an important role. A java thread should be at any point of following states at any time of exists. A thread lies only in one of the following shown states at any instant.

Thread life cycle

Before we dig into the Thread states, get an understanding of the basics of concurrent programming in my previous blog in here.

1. New

When a new thread is created, it will be in New state and thread has not yet started to run. When a thread is in New state, its code is yet to be run and has not started to execute.

Example: A new thread is created but not started, So leaving it like this,

Thread object is empty, and the resources are not available for the thread. If a user called any other method than start() it will occur an IllegalThreadStateExecption error.

2. Runnable

A thread that is ready to run is moved into the runnable state.

In this state, the thread might be running, or it is getting ready to run at any instant of time. The thread scheduler is responsible to give the time to run the thread. In most operating systems, each thread is given a small amount of processor time. When this happens, all such threads that are ready to run, waiting for the CPU and the currently running thread lies in runnable state.

3. Blocked

--

--

Royalist | Software Engineer | Postgraduate student in Big Data Analytics|