“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”

The life cycle of a thread refers to the entire process from creation to destruction of a thread. Generally, the life cycle of a thread can be divided into the following five types:

  • The initial state
  • Operational condition
  • Running state
  • A dormant state
  • Termination status

Their state transitions are shown below:

Java thread life cycle

The life cycle of a Java thread is different from the life cycle described above, and it has the following six states:

  1. NEW (initialization state)
  2. RUNNABLE (RUNNABLE/running state)
  3. The virtual drive is BLOCKED.
  4. WAITING (state without time limit)
  5. TIMED_WAITING (timed wait state)
  6. TERMINATED state

These six states can be found in Thread’s source code, as shown below:Of course, you can also use Java code to print all thread states, as shown below:

for (Thread.State value : Thread.State.values()) {
    System.out.println(value);
}
Copy the code

The execution result of the above program is as follows:

Lifecycle transition

Let’s talk about the Java thread lifecycle transition process.

1. From NEW to RUNNABLE

When we create a Thread, that is, a new Thread, the Thread is in the new state, as shown in the following code:

// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        // ...}});// Get the thread state
Thread.State state = thread.getState();
System.out.println(state);
Copy the code

The execution result of the above program is as follows: However, by calling the thread’s start method, the thread’s state changes from NEW to RUNNABLE, as shown in the following code:

// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        // Get the current thread of execution
        Thread currThread = Thread.currentThread();
        // Get the thread state
        Thread.State state = currThread.getState();
        // Prints thread statusSystem.out.println(state); }}); thread.start();Copy the code

The execution result of the above program is as follows:

2. From RUNNABLE to BLOCKED

When code in a thread queues synchronized, the thread changes from a RUNNABLE state to a BLOCKED state, as shown in the following code:

// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        try {
            // Wait 100 milliseconds
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Queue lock");
        synchronized (ThreadStates.class) {
        }
    }
});
thread.start();
// let the main thread get the lock first
synchronized (ThreadStates.class) {
    // Get the thread state
    Thread.State state = thread.getState();
    // Prints thread status
    System.out.println("First get thread state:" + state);
    / / sleep 1 s
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // Get the thread state again
    state = thread.getState();
    // Prints thread status
    System.out.println("Get thread state for second time:" + state);
}
Copy the code

The execution result of the above program is as follows: When a thread obtains a synchronized lock, it changes from BLOCKED to RUNNABLE.

3.从 RUNNABLE 到 WAITTING

After calling wait(), the thread changes from a RUNNABLE state to a WAITING state, as shown below:

// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        synchronized (this) {
            try {
                // Thread sleep
                this.wait();
            } catch(InterruptedException e) { e.printStackTrace(); }}}});// Start the thread
thread.start();
// Get the thread state
Thread.State state = thread.getState();
// Prints thread status
System.out.println("First get thread state:" + state);
/ / sleep 1 s
try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// Get the thread state
state = thread.getState();
// Prints thread status
System.out.println("Get thread state for second time:" + state);
Copy the code

The execution result of the above program is as follows: When notify/notifyAll is invoked, the thread changes from WAITING to RUNNABLE, as shown in the following code:

Object lock = new Object();
// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        synchronized (lock) {
            try {
                // Thread sleep
                lock.wait();
                // Get the current thread state
                Thread.State state = Thread.currentThread().getState();
                // Prints thread status
                System.out.println("Get thread status:" + state);
            } catch(InterruptedException e) { e.printStackTrace(); }}}});// Start the thread
thread.start();
// Get the thread state
Thread.State state = thread.getState();
// Prints thread status
System.out.println("First get thread state:" + state);
/ / sleep 1 s
try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// Get the thread state
state = thread.getState();
// Prints thread status
System.out.println("Get thread state for second time:" + state);

// Wake up the thread
synchronized (lock) {
    lock.notify();
}
Copy the code

The execution result of the above program is as follows:

4.从 RUNNABLE 到 TIMED_WATTING

When a wait method with timeout is called, such as sleep(XXX), the thread changes from RUNNABLE state to TIMED_WAITING timed state, as shown in the following code:

// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        try {
            Thread.sleep(1000);
        } catch(InterruptedException e) { e.printStackTrace(); }}});// Start the thread
thread.start();
// Get the thread state
Thread.State state = thread.getState();
// Prints thread status
System.out.println("First get thread state:" + state);
/ / sleep 1 s
try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// Get the thread state
state = thread.getState();
// Prints thread status
System.out.println("Get thread state for second time:" + state);
Copy the code

The execution result of the above program is as follows: When the timeout period is exceeded, the thread changes from TIMED_WAITING to RUNNABLE, the implementation code is as follows:

// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        try {
            Thread.sleep(1000);
            // Get the current thread state
            Thread.State state = Thread.currentThread().getState();
            // Prints thread status
            System.out.println("Get thread status:" + state);
        } catch(InterruptedException e) { e.printStackTrace(); }}});// Start the thread
thread.start();
// Get the thread state
Thread.State state = thread.getState();
// Prints thread status
System.out.println("First get thread state:" + state);
/ / sleep 1 s
try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// Get the thread state
state = thread.getState();
// Prints thread status
System.out.println("Get thread state for second time:" + state);
Copy the code

The execution result of the above program is as follows:

5. The RUNNABLE to TERMINATED

The thread is TERMINATED from the RUNNABLE state to the TERMINATED destruction state as shown in the following code:

// Create a thread
Thread thread = new Thread(new Runnable() {
    @Override
    public void run(a) {
        // Get the current thread state
        Thread.State state = Thread.currentThread().getState();
        // Prints thread status
        System.out.println("Get thread status:"+ state); }});// Start the thread
thread.start();
// Wait 100ms for the thread to finish executing
Thread.sleep(100);
// Get the thread state
Thread.State state = thread.getState();
// Prints thread status
System.out.println("Thread status:" + state);
Copy the code

The execution result of the above program is as follows:

conclusion

Threads in Java have six cycles of life: NEW (initial state), RUNNABLE (runable state), BLOCKED (BLOCKED state), WAITING (untimed wait state), TIMED_WAITING (TERMINATED state), and TERMINATED. The thread lifecycle transformation process is shown below:

The resources

Java Concurrent Programming

Judge right and wrong from yourself, praise to listen to others, gain and loss in the number.

Public number: Java interview analysis

Interview collection: gitee.com/mydb/interv…