Lifecycle of a thread in Java
A thread is a lightweight process in Java. A Java program always has at least one thread that is running called the main() thread. It can initiate many more threads from the main() thread.
States of a thread
A thread in Java could have the following states throughout its life cycle:
New
Active
Waiting/blocked
Timed waiting
Terminated / dead
The java.lang.Thread.State class
We can get the current state of the thread in Java by calling Thread.getState().
The java.lang.Thread.State class has an enum to represent the state of a thread that has the following values:
State | Declaration |
New |
|
Runnable |
|
Blocked |
|
Waiting |
|
Timed Waiting |
|
Let's learn about these states.
The new state
When a thread is created, it is in the new state. In this state, the thread has not yet started the execution of the code.
public class Main {public static void main(String[] args) {Thread1 obj = new Thread1();Thread thread = new Thread(obj);//State of thread before starting itSystem.out.println("State of thread before calling start: " + thread.getState());thread.start();}}
The active state
A thread goes into an active state when it is invoked by the start() method. In the active state, it can have further the following sub-states:
Runnable: A thread that is ready to run at a given time is moved to a runnable state, and it waits in the queue for its turn and time.
Running: When an active thread is executing and occupying the CPU, it is in the running state.
// ABC class implements the interface Runnableclass Thread1 implements Runnable{public void run(){}}
The waiting state
When a thread needs a resource that is occupied by another thread, it has to go into the waiting or blocked state. If a thread invokes the join() method, it goes into a waiting state and waits for the child thread to complete its execution.
public class Main {public static void main(String[] args) {try {Thread2.thread2 = new Thread(new Thread2());//starting thread2Thread2.thread2.start();}catch(Exception e){e.printStackTrace();}}}
The timed waiting state
In case a thread needs a resource that is being held by another thread or a process for a very long period of time, then the execution of this thread will remain blocked, and it will starve. To avoid this situation, sometimes timed waiting is used, for example, the thread is given a specific time to wait for that resource, and when that time limit reaches, it will stop waiting and continue its execution. One examples is using the sleep() method, which puts on a timed wait state to the current thread, and after the time is over, it continues its execution.
public class Main {public static void main(String[] args) {try{Thread thread = new Thread(new Thread1());//Start a thread and check its statethread.start();//wait for thread for a whilethread.join(20);System.out.println("Thread state after calling sleep inside it: " + thread.getState());}catch(Exception e){e.printStackTrace();}}}
The terminated state
A thread reaches the terminated state when it completes its execution or due to an unhandled exception for some external event. A terminated thread is also called dead because it can not be revoked or made alive.
public class Main {public static void main(String[] args) {try {Thread1 obj = new Thread1();Thread thread = new Thread(obj);//State of thread before starting itthread.start();//wait for thread to complete its executionthread.join();System.out.println("State of thread after calling start: " + thread.getState());}catch(Exception e){e.printStackTrace();}}}
Free Resources