Home  >  Article  >  The life cycle of a thread is divided into several stages

The life cycle of a thread is divided into several stages

青灯夜游
青灯夜游Original
2023-02-21 16:24:4113238browse

5 stages: 1. New, which is the thread that has just been created using the new method; 2. Ready, which is after calling the start() method of the thread. At this time, the thread is in the stage of waiting for the CPU to allocate resources; 3. Running, when the ready thread is scheduled and obtains CPU resources, it enters the running state; 4. Blocking, in the running state, the thread in the running state may become blocked due to some reasons; 5. Destruction , after the thread completes normal execution or the thread is forcibly terminated in advance or an exception occurs, the thread will be destroyed.

The life cycle of a thread is divided into several stages

The operating environment of this tutorial: Windows 7 system, Dell G3 computer.

The life cycle of a thread consists of 5 stages, including: new, ready, running, blocked, and destroyed. The complete life cycle diagram is as follows:

The life cycle of a thread is divided into several stages

When a thread enters the running state, the general operating system uses a preemptive method to allow the thread to obtain the CPU. Therefore, the CPU needs to switch between multiple threads, so the thread status will switch between running, blocked, and ready multiple times.

1. New (new)

##New : Use the new method, new comes out Thread, at this time, only the JAVA virtual machine allocates memory for it and initializes the values ​​​​of member variables. It is just an object at this time.

2. Ready (runnable)

Ready: It is called After the thread's start() method, the thread is in the stage of waiting for the CPU to allocate resources. Whoever grabs the CPU resources first will start execution; The thread enters the ready state, and
the JAVA virtual machine creates a method call stack for it and program counter . The execution of threads is controlled by the underlying platform and has a certain degree of randomness.

3. Running

##Running

: When the ready thread is scheduled When the CPU resource is obtained, it enters the running state. The run method defines the operation and function of the thread; (when the thread in the ready state obtains the CPU, it will execute the run() method) For a single-core CPU (or (a core), it can only execute one instruction at the same time, and the JVM achieves multi-threading by quickly switching threads to execute instructions. A real processor can process one instruction at the same time, but this switching speed is very fast, and we will not do it at all. perceived. In order to restore the correct execution position after thread switching, each thread has an independent program counter. The counters between each thread do not affect each other and are stored independently. When a thread starts running, it cannot hold the CPU all the time (unless the thread execution body is very short and the execution ends instantly). Therefore, the thread needs to be interrupted during execution in order to allow other threads to gain access to the CPU for execution. The details of thread scheduling depend on the strategy adopted by the underlying platform.

4. Blocked Blocked: In the running state, it may occur due to some reasons The running thread becomes blocked. The reasons are as follows:

1.等待I/O流的输入输出
2.等待网络资源,即网速问题
3.调用sleep()方法,需要等sleep时间结束
4.调用wait()方法,需要调用notify()唤醒线程
5.其他线程执行join()方法,当前线程则会阻塞,需要等其他线程执行完。

The state switching diagram is as follows:


The life cycle of a thread is divided into several stages

5. Destroyed (terminated )If the thread completes normal execution or the thread is forcibly terminated in advance or an exception occurs, the thread will be destroyed and resources will be released.

1. The run()/call() method is executed and the thread ends normally;

2. The thread throws an uncaught Exception or Error;

3. Directly call the thread's stop( ) method to end the thread - this method can easily lead to deadlock and is generally not recommended.

Extended knowledge: CPU time slice There is a crystal oscillator at the operating system level, which is a bit like a monk ringing a bell every few seconds. Collision occurs once in a short period of time, dividing the CPU time into time slices; each thread actually keeps grabbing time slices one by one; after the time slice reaches the point, it still has to grab it again (to ensure that all Threads have the opportunity to grab the CPU to execute their own logic; fairness)

New state

Let’s look at the following piece of code:

Thread t1 = new Thread();

The creation here is just It is created at the programming language level of JAVA, but at the operating system level, the real thread has not yet been created. Only when we call the start() method will the thread be created and enter the Runnable state. Only when we call the start() method will the thread be created

For more related knowledge, please visit the

FAQ

column!

The above is the detailed content of The life cycle of a thread is divided into several stages. 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