Home  >  Article  >  Java  >  An exploration into Java thread life cycle

An exploration into Java thread life cycle

PHPz
PHPzOriginal
2024-04-11 14:15:01640browse

Java threads go through the following life cycle stages: Creation: Created by inheriting the Thread class or implementing the Runnable interface. Preparation: After creation, the thread enters the preparation state and waits for scheduling execution. Running: Scheduled for execution, code is being executed. Blocking: Code cannot be executed when an obstacle is encountered. Wait: Actively wait for other threads. Sleep: Call the sleep() method to sleep, and code cannot be executed. Notification: wake up through notify() or notifyAll() method. Death: After execution is completed or an error occurs, execution can no longer be scheduled.

An exploration into Java thread life cycle

Exploring the life cycle of Java threads

In Java, a thread is a lightweight process with its own stack , state and execution context. Each thread has a unique life cycle, including the following stages:

1. Creation

The thread life cycle begins with creation and can be achieved in the following ways :

// 通过继承 Thread 类实现
Thread thread1 = new Thread() {
    public void run() {
        // 线程执行的代码
    }
};

// 通过实现 Runnable 接口实现
Runnable task = new Runnable() {
    public void run() {
        // 线程执行的代码
    }
};
Thread thread2 = new Thread(task);

2. Preparation

After a thread is created, it will enter the ready state. At this time, the thread has not yet been scheduled for execution.

3. Running

After the thread is scheduled to run, it will enter the running state. A thread in the running state is executing code.

4. Blocking

If a thread encounters an obstacle, such as waiting for a resource, it will enter the blocking state. At this point, the thread cannot execute code.

5. Waiting

Threads can actively wait for other threads. After calling the wait() method, the thread will enter the waiting state.

6. Sleep

The thread can also call the sleep() method to make it sleep for a certain period of time. During sleep, threads cannot execute code.

7. Notification

Waiting or sleeping threads can be called by calling the notify() or notifyAll() method. wake.

8. Death

When the thread execution is completed or an error occurs, it will enter the death state. A thread in a dead state can no longer be scheduled for execution.

Practical case

Create a thread and print the output:

class MyThread extends Thread {
    public void run() {
        System.out.println("Thread is running");
    }
}

public class Main {
    public static void main(String[] args) {
        MyThread thread = new MyThread();

        thread.start(); // 启动线程

        System.out.println("Thread state: " + thread.getState()); // 输出线程状态
    }
}

Output:

Thread is running
Thread state: RUNNABLE

This indicates that the thread is successfully created and Enter the running state.

The above is the detailed content of An exploration into Java thread life cycle. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn