Home > Java > javaTutorial > body text

Detailed explanation of the five states of Java threads and state transition rules

王林
Release: 2024-02-19 17:03:06
Original
549 people have browsed it

Detailed explanation of the five states of Java threads and state transition rules

In-depth understanding of the five states of Java threads and their conversion rules

1. Introduction to the five states of threads
In Java, the life cycle of a thread can It is divided into five different states, including new state (NEW), ready state (RUNNABLE), running state (RUNNING), blocked state (BLOCKED) and terminated state (TERMINATED).

  1. New state (NEW): When the thread object is created, it is in the new state. At this point, the thread object has allocated enough resources to perform the task, but has not yet started executing the run() method.
  2. Ready state (RUNNABLE): When the thread calls the start() method, the thread will enter the ready state. The thread in the ready state has the conditions to run and is waiting for the CPU to allocate a time segment to execute the code.
  3. Running state (RUNNING): When the thread in the ready state obtains the CPU time segment, it enters the running state and starts executing the code in the run() method. The thread will remain running until it terminates itself or is interrupted by another thread.
  4. Blocked state (BLOCKED): Under the following circumstances, the thread will enter the blocking state:

    • The sleep() method is called and the CPU resources are actively given up.
    • Waiting for the release of a resource, such as a locked resource.
    • Threads wait for the completion of other threads in the join() method.
  5. Terminated state (TERMINATED): After the thread ends, it enters the terminated state. After the thread completes the execution of the code in the run() method, or the thread is terminated due to exceptions or other reasons, the thread will enter the terminated state.

2. Conversion rules between states
There are certain conversion rules between thread states. Below we introduce the conversion rules between each state respectively.

  1. New state (NEW) is converted to ready state (RUNNABLE): When the thread object is created and the start() method is called, the thread is converted from new state to ready state.
  2. Convert the ready state (RUNNABLE) to the running state (RUNNING): When the thread obtains the CPU resources, it converts from the ready state to the running state.
  3. Converting the running state (RUNNING) to the blocking state (BLOCKED): During execution, the thread may enter the blocking state due to waiting for the release of a resource or actively releasing CPU resources.
  4. The blocking state (BLOCKED) is converted to the ready state (RUNNABLE): When the resource the thread is waiting for is released, or the waiting time is reached, it will be converted from the blocking state to the ready state.
  5. Converting the running state (RUNNING) to the terminated state (TERMINATED): When the thread's run() method is completed or the thread is terminated due to an exception, the thread will convert from the running state to the terminated state.

3. Code Example

The following is a simple code example, showing the conversion rules between thread states:

class MyThread extends Thread {
    
    @Override
    public void run() {
        try {
            Thread.sleep(1000);
            System.out.println("线程执行完毕");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class ThreadStateDemo {

    public static void main(String[] args) {
        MyThread thread = new MyThread();
        System.out.println("线程创建后状态:" + thread.getState());

        thread.start();
        System.out.println("调用start()方法后状态:" + thread.getState());

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("等待500ms后状态:" + thread.getState());

        try {
            thread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("调用join()方法后状态:" + thread.getState());
    }
}
Copy after login

Run the above code, you can see The output result is as follows:

线程创建后状态:NEW
调用start()方法后状态:RUNNABLE
等待500ms后状态:RUNNABLE
线程执行完毕
调用join()方法后状态:TERMINATED
Copy after login

The above code creates a thread object MyThread that inherits from the Thread class. In the main thread, we can observe the state changes of the thread object at different stages.

By having an in-depth understanding of the five states of Java threads and their conversion rules, we can better grasp the principles of multi-threading and further improve the ability of concurrent programming. At the same time, in the actual development process, the judgment and processing of thread status are also very important. I hope this article can be helpful to everyone.

The above is the detailed content of Detailed explanation of the five states of Java threads and state transition rules. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!