Thread state transition and code proof (just look at this article, with diagram and code verification)

background

Take you to understand the thread state in Java, with pictures and code display, all dry goods.

summary

Look at the State enumeration in the Thread class, which is the State of the Java Thread.
There are many states on the Internet. You can see that this enumeration is more authoritative. All kinds of nouns, what can be run, ready, messy, can be written in English.

  • New: the thread that just came out of new is in the new state before starting ()
  • Terminated: when the thread finishes running the code, it is in the terminated state
  • timed_waiting: timed means "with time". Waiting with time, such as the commonly used sleep(millis)
  • waiting: wait without time, such as join() and wait()
  • Blocked: for example, if you need a lock and can't get it, you enter the blocked state. For example, the synchronized block is blocked because it cannot obtain a lock

The runnable state is actually the most confusing
Most people have doubts, "runnable seems to be described incorrectly. Runnable means runnable in English words. It feels that runnable is closer to the commonly said ready state. In addition, is there no running state?"

runnable in Thread$State actually contains two states: one is the so-called ready state that has not been selected by the CPU, and the other is the running state that has been selected by the CPU.

PS: in fact, there is a view that runnable in Thread$State has three states: runnable, running, and blocked. The "blocking state" here is the blocking state at the operating system level, not the blocked state in the enumeration. The "blocking state" in the operating system is also classified as runnable.

What is the blocking state of the operating system? Refers to, for example, the blocking state when reading a file

Correspondence diagram of state transition and thread API

The code obtains various states in the thread

/**
 * Use a class to demonstrate various states of threads thread $state (the state of threads at the API level can be viewed from this enumeration)
 */
public class T05_ThreadState {
    public static void main(String[] args) {
        // new
        Thread t1 = new Thread(() -> {
        }, "t1");

        // runnable
        Thread t2 = new Thread(() -> {
            while (true) {

            }
        }, "t2");
        t2.start();

        // timed_waiting
        Thread t3 = new Thread(() -> {
            try {
                Thread.sleep(100*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t3");
        t3.start();

        // waiting
        Thread t4 = new Thread(() -> {
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t4");
        t4.start();


        // blocked
        Thread keepLock = new Thread(() -> {
            synchronized (T05_ThreadState.class) {
                while (true) {

                }
            }
        }, "keepLock");
        keepLock.start();

        // The main thread goes to sleep first and lets keepLock get the lock first
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Thread t5 = new Thread(() -> {
            synchronized (T05_ThreadState.class) {

            }
        }, "t5");
        t5.start();

        // terminated
        Thread t6 = new Thread(() -> {
            System.out.println("end");
        }, "t6");
        t6.start();

        // Take a nap and let t6 run to get the terminated status
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


        System.out.println(t1.getState());// new
        System.out.println(t2.getState());// runnable
        System.out.println(t3.getState());// timed_waiting
        System.out.println(t4.getState());// waiting
        System.out.println(t5.getState());// blocked
        System.out.println(t6.getState());// terminated
    }
}

Why is the thread in the IDEA RUNNING?

Why not runnable state?
Read the text in the picture in detail

Keywords: Java Back-end

Added by dixondwayne on Tue, 01 Mar 2022 06:37:44 +0200