Java thread state and life cycle

Schematic diagram of thread life cycle:



 

Status of the thread:

1. Initial state (New)

When a thread object instance is created, that is, new Thread(...), the thread enters the initial state. Note that at this time, it is just an ordinary object instance, not a real thread, and the JVM will not allocate corresponding resources. For example stack memory space.

 

2. Runnable/Ready

After calling the start() method, the thread is in the " runnable thread pool " and becomes runnable, that is, ready, just waiting to obtain the right to use the CPU . That is, except for the CPU, the process in the ready state has all the resources required to run.

 

3. Running status (Running)

The thread in the ready state is selected by the operating system, obtains the CPU time slice, and executes the program code to enter the running state.

If the thread in the running state executes the yield() method and assigns the CPU, it will return to the ready state. At this time, it will be in the same competition state with other threads. However, if the operating system does not ignore the priority setting of the thread, then the thread will be higher than the thread. Only the thread with high priority can get the scheduled execution of the CPU, if not, the thread will still be scheduled for execution again.

 

4. Blocked

The thread gives up the right to use the CPU for some reason and temporarily stops execution, which is the blocked state. The thread in the blocked state must re-enter the ready state before it has the opportunity to return to the running state. The blocking situations are as follows:

(1) Waiting for blocking (WAITING) : The running thread executes the wait() method, and the JVM releases all the resources occupied by the thread and puts it into the "waiting queue/waiting pool", and the thread entering this state cannot be automatically woken up , must rely on other threads to call notify() or notifyAll() method to be woken up.

(2), synchronous blocking ( BLOCKED ) : If the running thread finds that the synchronization lock has been occupied by other threads when it needs to acquire a synchronization lock (such as synchronized, etc.), the JVM will put the thread into the "lock pool" , and the lock All threads waiting for the same synchronization lock are stored in the pool in the form of a queue (ie, first come first served). Once the required synchronization lock object mark is obtained, it will be transferred to the ready state.

(3) Other blocking (TIMED_WAITING) : When the running thread calls its own sleep() method, or the join() method of other threads, or an I/O request is blocked, the JVM will place the thread in a blocking state . When the sleep() state times out, join() waits for the thread to terminate or times out, or when the I/O processing is complete, the thread re-enters the ready state.

(4) Thread suspension : After the running thread executes the suspend() method, the thread enters a blocked state and cannot be automatically resumed. It needs to call its resume() method to make the thread re-enter the ready state.

 

5. Terminated

 When the thread finishes executing or exits the run() method due to an exception, the thread ends its life cycle.

Guess you like

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