YouTip LogoYouTip

Thread Status

Java Example Java Example

In the lifecycle of a Java thread, there is an enumeration type State within the Thread class that defines several states of a thread:

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated

Thread Lifecycle

Explanation of each state:

1. Initial State - NEW

Declaration:

public static final Thread.State NEW

Implementing the Runnable interface or extending the Thread class gives you a thread class. When you create a new instance with new, the thread enters the initial state.

2. RUNNABLE

Declaration:

public static final Thread.State RUNNABLE

2.1. Ready State

The ready state simply means you are eligible to run. If the scheduler hasn't picked you, you remain in the ready state.

Calling the thread's start() method puts this thread into the ready state.

When the current thread's sleep() method ends, another thread's join() ends, waiting for user input completes, or a thread acquires an object lock, these threads will also enter the ready state.

When the current thread's time slice is used up, calling the current thread's yield() method puts the current thread into the ready state.

After a thread in the lock pool acquires the object lock, it enters the ready state.

2.2. Running State

This is the state a thread is in when the thread scheduler selects a thread from the runnable pool to be the current thread. This is also the only way for a thread to enter the running state.

3. Blocked State - BLOCKED

Declaration:

public static final Thread.State BLOCKED

The blocked state is when a thread is blocked while trying to enter a method or code block synchronized (acquiring a lock).

4. Waiting - WAITING

Declaration:

public static final Thread.State WAITING

Threads in this state are not allocated CPU execution time. They must wait to be explicitly woken up, otherwise they will remain in an indefinite waiting state.

5. Timed Waiting - TIMED_WAITING

Declaration:

public static final Thread.State TIMED_WAITING

Threads in this state are not allocated CPU execution time, but they do not need to wait indefinitely to be explicitly woken up by another thread. They will automatically wake up after a certain period of time.

6. Terminated State - TERMINATED

Declaration:

public static final Thread.State TERMINATED

We consider a thread terminated when its run() method completes, or when the main thread's main() method completes. This thread object may still be alive, but it is no longer a separately executing thread. Once a thread is terminated, it cannot be revived.

Calling the start() method on a terminated thread will throw a java.lang.IllegalThreadStateException.

The following example demonstrates how to get the state of a thread:

Main.java File

class thread 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 -"
            + Test.thread1.getState());
        try{
            Thread.sleep(200);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

public class Test implements Runnable{
    public static Thread thread1;
    public static Test obj;

    public static void main(String[] args){
        obj = new Test();
        thread1 = new Thread(obj);
        System.out.println("State of thread1 after creating it - " + thread1.getState());
        thread1.start();
        System.out.println("State of thread1 after calling .start() method on it - " + thread1.getState());
    }

    public void run(){
        thread myThread = new thread();
        Thread thread2 = new Thread(myThread);
        System.out.println("State of thread2 after creating it - "+ thread2.getState());
        thread2.start();
        System.out.println("State of thread2 after calling .start() method on it - " + thread2.getState());
        try{
            Thread.sleep(200);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("State of thread2 after calling .sleep() method on it - "+ thread2.getState());
        try{
            thread2.join();
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("State of thread2 when it has finished it's execution - " + thread2.getState());
    }
}

The output of running the above code is:

State of thread1 after creating it - NEW
State of thread1 after calling .start() method on it - RUNNABLE
State of thread2 after creating it - NEW
State of thread2 after calling .start() method on it - RUNNABLE
State of thread2 after calling .sleep() method on it - TIMED_WAITING
State of thread1 while it called join() method on thread2 - WAITING
State of thread2 when it has finished it's execution - TERMINATED

Java Example Java Example

← Met Element GetelementsbyclassThread Priorityinfo β†’