How do I get the state of a thread?

To get the state of a thread use getState() method of a Thread class. One thing to be noted is that this method is designed for use in monitoring of the system state, not for synchronization control.

package org.kodejava.example.lang;

public class GetThreadState implements Runnable {
    public void run() {
        System.out.println("Start..");
    }

    public static void main(String[] args) {
        Thread thread = new Thread(new GetThreadState());
        thread.start();

        //
        // Get the state of the thread.
        //
        Thread.State state = thread.getState();
        System.out.println("State: " + state.name());
    }
}
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada

Leave a Reply