The life cycle of a thread is divided into several stages
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 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:
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.
##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.
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.
: 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:
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
FAQThe 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

8-core means that the CPU has 8 physical cores, and 16-thread means that the CPU can have up to 16 threads processing tasks at the same time. The number of cores and threads are important performance indicators of a computer CPU. The higher the number of cores of the CPU, the higher the processing speed; the higher the number of threads, the more conducive it is to running multiple programs at the same time, because the number of threads is equivalent to the number of times the CPU can run at the same time at a certain moment. The number of tasks to be processed in parallel. Multi-threading can maximize wide-issue, out-of-order superscalar processing, improve the utilization of processor computing components, and alleviate memory access delays caused by data correlation or cache misses.

vue3 changed 4 life cycle functions. The Vue3 combined API cancels the beforeCreated and created hook functions and uses the step hook instead, and this cannot be used in it. The hook functions for component destruction in Vue3 have been changed from destroyed and beforeDestroy to beforeUnmount and unmounted.

In C++, function pointers require proper destruction and life cycle management. This can be achieved by manually destructing the function pointer and releasing the memory. Use smart pointers, such as std::unique_ptr or std::shared_ptr, to automatically manage the life cycle of function pointers. Bind the function pointer to the object, and the object life cycle manages the destruction of the function pointer. In GUI programming, using smart pointers or binding to objects ensures that callback functions are destructed at the appropriate time, avoiding memory leaks and inconsistencies.

The Servlet life cycle refers to the entire process from creation to destruction of a servlet, which can be divided into three stages: 1. Initialization stage, calling the init() method to initialize the Servlet; 2. Running stage (processing requests), the container will Request to create a ServletRequest object representing an HTTP request and a ServletResponse object representing an HTTP response, and then pass them as parameters to the service() method of the Servlet; 3. Destruction phase.

To avoid thread starvation, you can use fair locks to ensure fair allocation of resources, or set thread priorities. To solve priority inversion, you can use priority inheritance, which temporarily increases the priority of the thread holding the resource; or use lock promotion, which increases the priority of the thread that needs the resource.

Vue3 is currently one of the most popular frameworks in the front-end world, and the life cycle function of Vue3 is a very important part of Vue3. Vue3's life cycle function allows us to trigger specific events at specific times, enhancing the high degree of controllability of components. This article will explore and explain in detail the basic concepts of Vue3's life cycle functions, the roles and usage of each life cycle function, and implementation cases, to help readers quickly master Vue3's life cycle functions. 1. Vue3’s life cycle function

vue3的生命周期:1、beforeCreate;2、created;3、beforeMount;4、mounted;5、beforeUpdate;6、updated;7、beforeDestroy;8、destroyed;9、activated;10、deactivated;11、errorCaptured;12、getDerivedStateFromProps等等

Controlling the life cycle of a Go coroutine can be done in the following ways: Create a coroutine: Use the go keyword to start a new task. Terminate coroutines: wait for all coroutines to complete, use sync.WaitGroup. Use channel closing signals. Use context context.Context.