Thread life cycle and trigger mechanism

Five basic states of java thread
1. New state (new): When the thread object is created, it enters the new state, such as Thread t=new MyThread();
2. Ready state (Runnable): when the start( ) method (t.start()), the thread enters the ready state, and the thread in the ready state is processed. It just means that the current thread is ready and ready to wait for the CPU to schedule execution at any time. It does not mean that the execution of t.start() will immediately Execution
3. Running state (Running): When the CPU starts to schedule the thread in the ready state, the thread at this time starts to be executed, that is, enters the running state. PS: The ready state is the only entry to enter the running state, that is to say , if the thread wants to enter the running state, it must first be in the ready state
4. Blocked state (Blocked): For some reason, the thread in the running state temporarily gives up the right to use the CPU and stops executing. At this time, it enters the blocked state until it enters In the ready state, there is a chance to be called by the CPU again to enter the ready state. According to different causes of blocking, the blocking state can be divided into three types:
waiting blocking: running state thread executes wait()
synchronous blocking: thread fails to acquire synchronized synchronization lock (Because the lock is occupied by other threads), it will synchronously enter the synchronization lock to block
other blocks: by calling the thread's sleep() or join() or sending an I/O request, the thread will enter the blocked state, when the sleep() state Timeout, join() waits for the timeout thread to terminate or timeout, or the I/O processing is completed, and the thread re-enters the ready state 5. Death state
: the thread is executed or exits the run() method due to an exception, the thread life

The ready state changes to the running state: the current thread acquires processor resources. The
running state changes to the ready state: the current thread calls yeid() or loses processor resources during the running process. The
running state changes to the dead state: the current thread finishes executing or occurs abnormal

ps: When the thread's yield() method is called, the thread transitions from the running state to the ready state, but which thread in the ready state is scheduled by the CPU has a certain degree of randomness. Therefore, it may appear that the A thread calls yield() method, then the CPU still schedules the A thread.

 

Guess you like

Origin blog.csdn.net/m1195900241/article/details/126380215