Learnitweb

Java Thread Life Cycle and Thread States

1. Introduction

From creation to termination, a thread can be in one of the five states. A thread can only be in one state at a time. These states are with reference to JVM and not operating system.

Thread can be in any of the following states:

2.1 New

A thread is in NEW state when a thread instance is created but not started yet.

2.2 Runnable

A thread is in RUNNABLE state when it is running in the Java virtual machine. A thread in RUNNABLE state does not mean it is actually being executed.

Thread scheduler is responsible to pick one of the runnable threads and provide it time to run – often called timeslice. When time is provided by thread to run, the thread runs for its time and is said to be running. And then it relinquishes the CPU so that another thread could run and becomes in RUNNABLE state again. All such threads which are eligible to be picked up thread scheduler and are said to be in RUNNABLE state.

2.3 Blocked

A thread is in BLOCKED state if it is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method after calling Object.wait.

A blocked thread first becomes a runnable thread and then it is picked up for running. When a thread enters a synchronized block of code, it takes a lock on the object. If another thread also wants to execute the same section of code, it will have to wait. While waiting, the thread is said to be in the BLOCKED state.

2.4 Waiting

A thread is in WAITING state it is waiting for another thread to perform some action. A thread is in the WAITING state due to the calling of any of the following methods:

  • Object.wait with no timeout
  • Thread.join with no timeout
  • LockSupport.park

2.5 Timed Waiting

A thread is in TIMED WAITING state if it is in waiting state with a specified waiting time. A thread is in TIMED WAITING state due to the calling of any of the following methods:

  • Thread.sleep
  • Object.wait with timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil

2.6 Terminated

A thread is in a TERMINATED state once it completes execution. A thread may also be in TERMINATED state due to some error or unhandled exception or forcefully killed. The terminated thread does not consume any CPU cycle.

3. Enum Thread.State

A thread state in Java is represented by Enum Thread.State. Enum constants representing thread states are:

  • NEW
  • RUNNABLE
  • BLOCKED
  • WAITING
  • TIMED_WAITING
  • TERMINATED

You can’t modify the status of a thread. The Thread class does not have method to set status explicitly.

4. Program to demonstrate thread states

In the following program we are creating two threads and then synchronizing threads to observe thread states.

package com.learnitweb;

class MyThread implements Runnable {
	public void run() {
		try {
			Thread.sleep(1500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println(
				"State of thread1 while it called join() method on thread2: " + ThreadStateExample.thread1.getState());
		try {
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

public class ThreadStateExample implements Runnable {

	public static ThreadStateExample tseObj = new ThreadStateExample();
	public static Thread thread1 = new Thread(tseObj);

	public static void main(String[] args) {

		// Before starting thread1 is in NEW state
		System.out.println("State of thread1 before starting it: " + thread1.getState());
		thread1.start();

		// After start thread1 is in RUNNABLE state
		System.out.println("State of thread1 after start(): " + thread1.getState());
	}

	public void run() {
		MyThread myThread = new MyThread();
		Thread thread2 = new Thread(myThread);

		// Before starting thread2 is in NEW state
		System.out.println("State of thread2 before starting it: " + thread2.getState());
		thread2.start();

		// After calling start() thread2 is in RUNNABLE state
		System.out.println("State of thread2 after start(): " + thread2.getState());

		try {
			// thread1 will be in timed waiting state
			Thread.sleep(200);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("State of thread2 after calling sleep() method: " + thread2.getState());

		try {
			thread2.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("State of thread2 after execution: " + thread2.getState());
	}

}

Output

State of thread1 before starting it: NEW
State of thread1 after start(): RUNNABLE
State of thread2 before starting it: NEW
State of thread2 after start(): RUNNABLE
State of thread2 after calling sleep() method: TIMED_WAITING
State of thread1 while it called join() method on thread2: WAITING
State of thread2 after execution: TERMINATED