002 The state of the thread

I. Overview

  In the life cycle of a thread, it will be transformed by different states, and understanding the concepts of these states can help to understand the running process of the thread.


 

2. Thread status

In the Thread class, there is an inner class Status (actually an enumeration class), which describes the status of the thread.

public enum State {      
        NEW,
        RUNNABLE,
 
        BLOCKED,
      
        WAITING,
       
        TIMED_WAITING,

        TERMINATED;
    }

The six states of the thread are defined in this class. Among them, the runnable state includes our running state and ready state, because the switching of these two states is determined by the JVM, we can understand it as a state.

[1]NEW: The state after the thread object is created, when the thread does not call the start() method.

[2] Runnable: The thread object is in a runnable state, and whether it is running depends on whether the time slice has been allocated.

[3] Block: Blocking state. Generally, the thread enters the blocking queue due to the failure of the thread to snatch the lock resource.

[4]wating: Waiting state, generally the thread itself gives up the time slice and enters the waiting state. When no other thread wakes up, this thread is always in the waiting queue.

[5] time-wait: Sleep state, generally the state that the thread itself enters into sleep actively. When the time elapses, the thread automatically enters the runnable state.

[6]terminated: Terminated state, generally a state entered by a thread exception or complete operation.

Guess you like

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