Easy Tutorial
❮ Arrays Compare Java Override Overload ❯

Java Example - Get Thread State

Java Examples

In the lifecycle of a Java thread, the Thread class has an enumeration type State that defines several states of a thread, which include:

State descriptions:

1. Initial State - NEW

Declaration:

public static final Thread.State NEW

Implementing the Runnable interface and inheriting from the Thread class can produce a thread class. Creating an instance of this class puts the thread into the initial state.

2. RUNNABLE

Declaration:

public static final Thread.State RUNNABLE

2.1. Ready State

The ready state means the thread is eligible to run, but the scheduler has not selected it yet. It remains in the ready state until selected.

Calling the start() method on a thread puts it into the ready state.

When the current thread's sleep() method ends, other threads' join() methods end, user input is completed, or a thread acquires an object lock, these threads also enter the ready state.

When the current thread's time slice is used up and the yield() method is called on the current thread, it enters the ready state.

A thread in the lock pool acquires an object lock and enters the ready state.

2.2. Running State

The thread scheduler selects a thread from the runnable pool to be the current thread, putting it into the running state. This is the only way a thread enters 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 synchronized method or block (acquiring a lock) decorated with the synchronized keyword.

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 awakened; otherwise, they 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. However, they do not need to wait indefinitely; they will automatically wake up after a certain period.

6. Terminated State - TERMINATED

Declaration:

public static final Thread.State TERMINATED

A thread is considered terminated when its run() method completes, or when the main() method of the main thread completes. Although the thread object may still exist, it is no longer a separate executing thread. Once terminated, a thread cannot be restarted.

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

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

Main.java File

// Java program - Demonstrate thread states
class ThreadExample implements Runnable 
{ 
    public void run() 
    { 
        // Thread2 - Timed Waiting
        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);

        // Thread1 created and is currently in the NEW state.
        System.out.println("State of thread1 after creating it - " + thread1.getState());
        thread1.start();

        // Thread1 moved to Runnable state
        System.out.println("State of thread1 after calling start() method on it - " + 
            thread1.getState());
    } 

    public void run() 
    { 
        ThreadExample threadExample = new ThreadExample();
        Thread thread2 = new Thread(threadExample);

        // Thread2 created and is currently in the NEW state.
        System.out.println("State of thread2 after creating it - " + thread2.getState());
        thread2.start();

        // Thread2 moved to Runnable state
        System.out.println("State of thread2 after calling start() method on it - " + 
            thread2.getState());

        // Moving thread1 to timed waiting state
        try
        { 
            // Making thread1 sleep for 2000 ms
            Thread.sleep(2000);
        }  
        catch (InterruptedException e)  
        { 
            e.printStackTrace(); 
        }
        System.out.println("State of thread2 after calling sleep() method on it - " + 
            thread2.getState() );

        try 
        { 
            // waiting for thread2 to die
            thread2.join();
        }  
        catch (InterruptedException e)  
        { 
            e.printStackTrace(); 
        }
        System.out.println("State of thread2 when it has finished it's execution - " + 
            thread2.getState());
    } 

}
obj = new Test();
thread1 = new Thread(obj);

// Create thread1, currently in the new state
System.out.println("State of thread1 after creating it - " + thread1.getState());
thread1.start();

// thread1 - ready state
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);

    // Create thread2, currently in the new state
    System.out.println("State of thread2 after creating it - " + thread2.getState());
    thread2.start();

    // thread2 - ready state
    System.out.println("State of thread2 after calling .start() method on it - " +
        thread2.getState());

    // Moving thread2 to timed waiting state
    try
    {
        // Moving to timed waiting
        Thread.sleep(200);
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    System.out.println("State of thread2 after calling .sleep() method on it - " +
        thread2.getState());

    try
    {
        // Waiting for thread2 to terminate
        thread2.join();
    }
    catch (InterruptedException e)
    {
        e.printStackTrace();
    }
    System.out.println("State of thread2 when it has finished its execution - " +
        thread2.getState());
}

}

The above code outputs:

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 its execution - TERMINATED

Java Examples ```

❮ Arrays Compare Java Override Overload ❯