Now let’s look at the complete lifecycle of the thread, as shown in the following figure:New – Ready – Run – Block – death five processes. Let’s introduce them one by one:
- New: Newly created is still in memory, not yet in the schedulable thread pool, and cannot be scheduled by the CPU to perform work.
- Ready: The scheduling pool is entered and can be scheduled.
- Run: The CPU is responsible for scheduling threads in the “ready” state in the schedulable thread pool. Threads may switch back and forth between “ready” and “run” before execution. The switch between ready and running is done by the CPU, and the programmer cannot interfere
- Block: A running thread, which can block execution with sleep or lock when certain conditions are met, is removed from the scheduling pool and placed in memory without execution.
- Death: There are two cases. Normal death, thread execution ends. Unnatural death, sudden crash of the program/when a condition is met, force the thread to exit from within the thread, calling the exit method.
The role and summary of the exit method
- Causes the current thread to exit.
- This method cannot be called on the main thread; it will cause the main thread to exit.
- When the current thread dies, none of the code in that thread is executed.
- Before calling this method, be careful to release the objects previously created by the C framework.
In this paper, we seek knowledge, think carefully and wuhao,
Recommended from the previous article: Basic understanding of multithreading
What are the ways to implement multithreading?