Ali Ermian: Tell me about the thread life cycle and the conversion process?

The life cycle of a thread refers to the entire process from creation to destruction of a thread. Generally, there are the following five life cycles of a thread:

  • initial state
  • operational state
  • Operating status
  • sleep state
  • termination state

Their state transitions are shown in the following diagram:

Java thread life cycle

The life cycle of a Java thread is different from the life cycle mentioned above. It has the following six states:

  1. NEW (initialization state)
  2. RUNNABLE (runnable/running state)
  3. BLOCKED (blocked state)
  4. WAITING (unlimited waiting state)
  5. TIMED_WAITING (timed waiting state)
  6. TERMINATED (terminated status)

We can find these 6 states in the source code of Thread, as follows:

Of course, you can also use Java code to print all thread status, as shown in the following code:

for (Thread.State value : Thread.State.values()) {
    System.out.println(value);
}
复制代码

The execution result of the above program is shown in the following figure:

Lifecycle transition

Next, let's talk about the conversion process of the Java thread life cycle.

1. From NEW to RUNNABLE

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

// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        // ...
    }
});
// 获取线程状态
Thread.State state = thread.getState();
System.out.println(state);
复制代码

The execution result of the above program is shown in the following figure:

However, after calling the start method of the thread, the state of the thread changes from NEW to RUNNABLE , as shown in the following code:

// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        // 获取到当前执行的线程
        Thread currThread = Thread.currentThread();
        // 获取线程状态
        Thread.State state = currThread.getState();
        // 打印线程状态
        System.out.println(state);
    }
});
thread.start();
复制代码

The execution result of the above program is shown in the following figure:

2. From RUNNABLE to BLOCKED

When the code in the thread is queued to execute synchronized, the thread changes from the RUNNABLE state to the BLOCKED blocking state , as shown in the following code:

// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            // 等待 100 毫秒
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("排队使用锁");
        synchronized (ThreadStates.class) {
        }
    }
});
thread.start();
// 让主线程先得到锁
synchronized (ThreadStates.class) {
    // 获取线程状态
    Thread.State state = thread.getState();
    // 打印线程状态
    System.out.println("首次获取线程状态:" + state);
    // 休眠 1s
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    // 再次获取线程状态
    state = thread.getState();
    // 打印线程状态
    System.out.println("第二次获取线程状态:" + state);
}
复制代码

The execution result of the above program is shown in the following figure:

When a thread acquires a synchronized lock, it transitions from the BLOCKED state to the RUNNABLE state.

3. From RUNNABLE to WAITTING

After the thread calls the wait() method, it changes from the RUNNABLE state to the WAITING indefinite waiting state, as follows:

// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        synchronized (this) {
            try {
                // 线程休眠
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
});
// 启动线程
thread.start();
// 获取线程状态
Thread.State state = thread.getState();
// 打印线程状态
System.out.println("首次获取线程状态:" + state);
// 休眠 1s
try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// 获取线程状态
state = thread.getState();
// 打印线程状态
System.out.println("第二次获取线程状态:" + state);
复制代码

The execution result of the above program is shown in the following figure:

When the notify/notifyAll method is called, the thread will change from the WAITING state to the RUNNABLE state , as shown in the following code:

Object lock = new Object();
// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        synchronized (lock) {
            try {
                // 线程休眠
                lock.wait();
                // 获取当前线程状态
                Thread.State state = Thread.currentThread().getState();
                // 打印线程状态
                System.out.println("获取线程状态:" + state);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
});
// 启动线程
thread.start();
// 获取线程状态
Thread.State state = thread.getState();
// 打印线程状态
System.out.println("首次获取线程状态:" + state);
// 休眠 1s
try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// 获取线程状态
state = thread.getState();
// 打印线程状态
System.out.println("第二次获取线程状态:" + state);

// 唤醒 thread 线程
synchronized (lock) {
    lock.notify();
}
复制代码

The execution result of the above program is shown in the following figure:

4. From RUNNABLE to TIMED_WATTING

When calling a wait method with a timeout, such as sleep(xxx), the thread will change from the RUNNABLE state to the TIMED_WAITING state, as shown in the following code:

// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
});
// 启动线程
thread.start();
// 获取线程状态
Thread.State state = thread.getState();
// 打印线程状态
System.out.println("首次获取线程状态:" + state);
// 休眠 1s
try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// 获取线程状态
state = thread.getState();
// 打印线程状态
System.out.println("第二次获取线程状态:" + state);
复制代码

The execution result of the above program is shown in the following figure:

When the timeout period is exceeded, the thread will change from the TIMED_WAITING state to the RUNNABLE state . The implementation code is as follows:

// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            // 获取当前线程状态
            Thread.State state = Thread.currentThread().getState();
            // 打印线程状态
            System.out.println("获取线程状态:" + state);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
});
// 启动线程
thread.start();
// 获取线程状态
Thread.State state = thread.getState();
// 打印线程状态
System.out.println("首次获取线程状态:" + state);
// 休眠 1s
try {
    Thread.sleep(100);
} catch (InterruptedException e) {
    e.printStackTrace();
}
// 获取线程状态
state = thread.getState();
// 打印线程状态
System.out.println("第二次获取线程状态:" + state);
复制代码

The execution result of the above program is shown in the following figure:

5.RUNNABLE 到 TERMINATED

After the thread is executed, it will change from the RUNNABLE state to the TERMINATED destruction state, as shown in the following code:

// 创建线程
Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        // 获取当前线程状态
        Thread.State state = Thread.currentThread().getState();
        // 打印线程状态
        System.out.println("获取线程状态:" + state);
    }
});
// 启动线程
thread.start();
// 等待 100ms,待线程执行完
Thread.sleep(100);
// 获取线程状态
Thread.State state = thread.getState();
// 打印线程状态
System.out.println("线程状态:" + state);
复制代码

The execution result of the above program is shown in the following figure:

Summarize

There are 6 kinds of life cycles of threads in Java: NEW (initialization state), RUNNABLE (runnable/running state), BLOCKED (blocking state), WAITING (unlimited waiting state), TIMED_WAITING (timed waiting state), TERMINATED (termination) state). The conversion process of the thread life cycle is shown in the following figure:


Original link: https://juejin.cn/post/7065856076737937438
 

Guess you like

Origin blog.csdn.net/wdjnb/article/details/123004081