Describe the life cycle of Java threads in detail

     Everyone is welcome to search for "Technical Notes of Little Monkey" and follow my official account. If you have any questions, you can communicate with me in time.


    A thread will go through its own life cycle from creation to death. We may often see the "Running" state of the thread in blogs, but you can't find the "Running" state by reading the source code of the Thread class, then in the Java thread Is there a Running state?

     First of all, by looking at the source code provided by "Thread", we can see that the life cycle of a thread will go through the following states (note that at the same time, a thread can only be in one of these states):

public enum State {
    // The state when the thread has not been started
    NEW,
    // Indicates that the "start()" method is called, which can also be subdivided into "ready" and "running" states
    RUNNABLE,
    // The thread waits to enter the synchronized method or waits to enter the synchronized block
    BLOCKED,
    // Indicates that the thread enters a waiting state, such as calling "wait()", "join()", etc.
    WAITING,
    // with timeout
    TIMED_WAITING,
    // The thread ends normally, or is forced to stop, or the state after abnormal termination is encountered
    TERMINATED;
}

    NEW: You can understand by reading the source code comments provided by the official, "NEW" indicates the state when the thread has not been started. So what is it that hasn't been activated yet? Let's assume there is a thread class that implements the Runnable interface.

public class ThreadState implements Runnable {
    @Override
    public void run() {
    }
}

     A thread is constructed below, but the "start()" method has not been called. Before the "start" method is called, the thread is in the "NEW" state.

public class ThreadStateTest {
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadState());
    }
}

    RUNNABLE: Indicates that the thread called the "start()" method. In fact, the Runnable state here can be divided into two other states, namely the ready state and the running state.

public class ThreadStateTest {
    public static void main(String[] args) {
        Thread thread = new Thread(new ThreadState());
        thread.start();
    }
}

    Because the startup of the thread needs to be selected by the CPU before it can run, so for the ready state: it means that the thread has been called, that is, the "start()" method has been called, but it has not really run, and it needs to wait for the CPU to schedule and execute it. , enter the "Running" state, that is, the thread is executing.

    Blocked: The thread waits to enter the synchronized method or wait to enter the synchronized block.

    WAITING: Waiting state, indicating that the thread enters the waiting state, such as calling "wait()", "join()" and other methods without timeout. The waiting thread at this moment can only be awakened by other threads, and the thread in the waiting state must first enter the "ready (ready)" state before entering the "Running" state (because this involves the thread waking up the waiting thread, yes Random) For example, in the example shown in the previous article introducing the basic usage of wait and notify, calling "this.wait()" is to make the current thread enter the waiting state.

public void addMoney(int money) throws InterruptedException {
    synchronized (this) {
        while (balance <= money) {
            balance += money;
            System.out.println("Mom: Deposited into the account:" + money + "Yuan, the total account amount is:" + balance + "Yuan");
            this.notify();
            this.wait();
        }
    }
}

    TIMED_WAITING: The timeout waiting state, compared with the WAITING state, it can return by itself within a specified time. For example, calling "sleep(long time)", "wait(long)", "join(long)" and other methods with timeout, they will automatically return within a period of time.

public final native void wait(long timeout) throws InterruptedException;

     Then the thread will return from the waiting state and continue to execute.

    TERMINATED: The thread is in a state of death. For example, after executing the "run()" method, it ends normally. Forced termination, calling "stop()" or "destory()", etc. Abnormal termination: An exception occurred during execution.

     By reading the source code, we can see that Java does not explicitly give "Running state", but merges "Running" (running) and "Ready (ready)" in the operating system into "Runable (running state)"

     Here is a very classic thread life cycle diagram for everyone, yours! You carefully taste! Finally, the book "The Art of Java Concurrent Programming" is highly recommended! ! !

Posted by tryin_to_learn on Sun, 01 May 2022 18:47:07 +0300