Java multithreading – the life cycle of threads
The life cycle of a thread is divided into five stages: new, runnable, running, blocked and dead.
After the thread is started, with the CPU scheduling, the thread will switch between running and blocking.
New: after creating a thread with the new keyword, the thread will be in the new state;
Ready: after the thread object calls the start method, the thread is in a ready state. At this time, whether the thread can run depends on the scheduling of the JVM thread scheduler;
Run: after the thread obtains the CPU, it enters the running state;
Blocking: threads that do not die enter the blocking state after losing CPU;
Death: the execution of thread run () method is completed; The thread throws an uncapped exception or error; The thread actively calls the stop () method to end the current thread.
Thread from run to block:
Thread from blocking to ready state:
Thread state transition diagram:
About thread death: