Thread state

A thread has five states

Thread.State

Example thread status code:

public class ThreadState implements Runnable {

    @Override
    public void run(a) {
        for (int i = 0; i < 1; i++) {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException interruptedException) {
                interruptedException.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().getName() + "In process");
    }

    public static void main(String[] args) throws InterruptedException {
        Runnable target;
        Thread thread = new Thread(new ThreadState());
        // Not started
        System.out.println(thread.getState());
        // Start the thread
        thread.start();
        System.out.println(thread.getState());


        while(thread.getState() ! = Thread.State.TERMINATED) { Thread.sleep(100); System.out.println(thread.getState()); } System.out.println(thread.getState()); }}Copy the code

Execution Result: