Thread life cycle and state transition details

(1) New state - (2) Ready state - ((4) Blocking state) - (3) Running state - (5) Dead state 
Thread life cycle and state transition 
(1) New: After creating a thread object , the thread is in a new state, this When it cannot run, like other Java objects, it is only allocated memory by the Java virtual machine, and does not show any dynamic characteristics of the thread; 
(2) Runnable: After the thread object calls the start() method, the thread enters the the ready state (also known as the runnable state). The thread in the ready state is located in the runnable pool. At this time, it only has the conditions for running. Whether it can obtain the right to use the CPU to start running, it still needs to wait for the scheduling of the system; 
(3) Running: The thread in the ready state has obtained the The CPU usage right starts to execute the thread execution body in the run() method, and the thread is in the running state. When a thread is started, it cannot be running all the time (unless its thread execution body is short enough to end instantly), when the time allocated by the system is used up, the system will strip the CPU resources occupied by the thread and let other threads obtain opportunity to execute. Only threads in the ready state may transition to the running state. 
(4) Blocked: In some special cases, an executing thread will give up the right to use the CPU and enter a blocked state when performing time-consuming input/output operations. After the thread enters the blocking state, it cannot enter the queue. The thread can enter the ready state only after the cause of the reference blocking has been eliminated. 
——When a thread tries to acquire the synchronization lock of an object, if the lock is held by other threads, the current thread enters the blocking state. If it wants to enter the ready state from the blocking state, it must acquire the locks held by other threads. . 
- When a thread calls a blocking IO method, the thread will enter the blocking state. If you want to enter the ready state, you must wait until the blocking IO method returns. 
- When a thread calls the wait() method of an object, it will also cause the thread to enter a blocking state, and the notify() method will wake up. 
- Thread's sleep (long millis) is called. When the thread sleep time is up, it will automatically enter the blocking state. 
- When a thread calls the join() method of another thread, the current thread enters the blocking state. When the newly added thread finishes running, it will end the blocking state and enter the ready state. 
The thread can only enter the ready state from the blocking state, but cannot directly enter the running state, that is, the thread that ends the blocking needs to re-enter the runnable pool and wait for the system to schedule. 
(5) Terminated: When the run() method of the thread is executed normally or the thread throws an uncaught exception (Exception) or error (Error), the thread enters the dead state. Once in the dead state, the thread is no longer eligible to run and cannot transition to another state.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326396314&siteId=291194637