Thread is a dynamic execution process. It has a process from birth to death, with a total of five states:
New Thread (new Thread) ##Ready (runnable)
The thread has been started and is waiting to be assigned to the CPU time slice, which means that the thread is queuing in the ready queue waiting to get CPU resources. . For example: t1.start();Running(running)
The thread has obtained the CPU resource and is executing the task (run() method). At this time, unless this thread automatically If the CPU resource is given up or a thread with a higher priority enters, the thread will run until the end.Death(dead)
When the thread completes execution or is killed by other threads, the thread enters the death state. At this time, the thread cannot enter the ready state and wait for execution. .Natural termination: Terminate after running the run() method normally
Abnormal termination: Call the stop() method to stop a thread from runningBlocked (blocked)
For some reason, the running thread gives up the CPU and suspends its own execution, that is, it enters a blocking state.Sleeping: Use the sleep(long t) method to put the thread into sleep mode. A sleeping thread can enter the ready state after the specified time has elapsed.
Waiting: Call the wait() method. (Call the motivate() method to return to the ready state) Blocked by another thread: call the suspend() method. (Call the resume() method to restore).The above is the detailed content of There are several states of java threads. For more information, please follow other related articles on the PHP Chinese website!