A Thread has its own life cycle from creation to death. You can see the “Running” state of a Thread in a blog, but you cannot find the “Running” state by reading the source code of the Thread class.

The Thread life cycle goes through the following states (note that a Thread can only be in one of these states at a time) :

public enum State {
    // The status of the thread before it has been started
    NEW,
    // indicates that the "start()" method is called, which can be subdivided into "ready" and "running" states
    RUNNABLE,
    The thread waits to enter a synchronized method or to enter a synchronized block
    BLOCKED,
    // Indicates that the thread enters the wait state, such as calling "wait()", "join()", etc
    WAITING,
    // With a timeout
    TIMED_WAITING,
    // The thread terminates normally, or forcibly stops, or encounters an abnormal termination
    TERMINATED;
}
Copy the code

NEW: Read the source code comment provided by the official, “NEW” indicates the state of the thread before it has been started. So what hasn’t been activated yet? Let’s say we have a thread class that implements the Runnable interface.


public class ThreadState implements Runnable {
    @Override
    public void run(a) {}}Copy the code

Below, a thread is built, but the “start()” method has not been called. Until 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(newThreadState()); }}Copy the code

RUNNABLE: indicates that the thread has called the start() method. The RUNNABLE state can be divided into two other states: ready state and running state.

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

The ready state means that the thread has been called, that is, the “start()” method has been called, but it has not run yet. It needs to wait for the CPU to schedule the thread to execute and then enter the “Running” state, that is, the thread is executing.

Blocked: A thread waits to enter a synchronized method or block.WAITING: indicates that the thread is in a WAITING state. For example, wait() or join() is invoked without timeout. The waiting thread can only be woken up by another thread, and the waiting thread must enter the “ready” state before entering the “Running” state (because this involves the thread waking up the waiting thread, Random) For example, when we introduced the basic uses of wait and notify in the previous article, we called “this.wait()” to put the current thread into a wait state.

public void addMoney(int money) throws InterruptedException {
    synchronized (this) {
        while (balance <= money) {
            balance += money;
            System.out.println("Mom: Put it in an account:" + money + "Yuan, the total amount of the account is: + balance + "Yuan");
            this.notify();
            this.wait(); }}}Copy the code

TIMED_WAITING: timeout WAITING state. Compared to WAITING state, this state can return within a specified time. For example, methods such as “sleep (long time)”, “wait (long)”, and “Join (long)” with timeout times are automatically returned within a certain period of time.

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

The thread will then return from the wait state and continue executing.

TERMINATED: State of thread death. TERMINATED normally, for example, after the run() method is executed. Forced termination, calling “stop()” or “destory()” etc. Abnormal termination: An exception occurs during execution.

Reading the source code, Java does not specify “Running state “. Instead, it combines “Running” and “Ready” in the operating system into “Runable”.

Welcome to search “little monkey technical notes” to follow my public account, you can timely communicate with me if you have any questions.

Here to give you a very classic thread life cycle diagram, you taste! You taste carefully! Finally, I highly recommend the art of Java Concurrent Programming!!