Understand the state transition mechanism at each node in the lifecycle.

Common thread life cycle

  • Initial state: a thread that has been created but is not yet allowed to allocate CPU for execution. (Here the threads are created only at the programming language level; at the operating system level, the actual threads are not created yet.)
  • Runnable: Indicates that the thread can allocate CPU for execution. In this state, the actual operating system thread has been successfully created, so CPU can be allocated for execution.
  • When a free CPU is available, the operating system allocates it to a runnable thread, and the state of the thread allocated to the CPU changes to the running state.
  • If a running thread calls a blocking API (such as blocking to read a file) or waits for an event (such as a condition variable), the thread transitions to sleep and releases CPU usage. The sleep thread never gets the chance to acquire CPU usage. When a waiting event occurs, the thread transitions from sleep to runnable state.
  • The thread will enter the terminated state when it finishes execution or occurs an exception. The terminated state thread will not switch to any other state. Entering the terminated state means that the life cycle of the thread is over.

The Java language incorporates runnable state runnable states, which are useful at the operating system scheduling level, but which main didn’t care about because the JVM left thread scheduling to the operating system.

The life cycle of threads in Java and their state transitions



○ NEW: Indicates the initialization state

○ RUNNABLE: Running status

○ BLOCKED: The virtual drive is BLOCKED

○ WAITING: WAITING without time limit

○ TIMED_WAITING: Waiting with a time limit

○ TERMINATED: indicates the TERMINATED state

In fact, in the operating system main, the Java thread (four-color part) three is a state, namely sleep state, can also be understood as the three causes of thread sleep state. This means that as long as a Java thread is in one of these three states, it will never have CPU access.

2.1 State conversion between RUNNABLE and BLOCKED

Only one scenario that triggers this transition is when a thread waits for synchronized implicit locks. (When acquired)Copy the code

2.2 State Conversion between RUNNABLE and WAITING

Scenario 1: A thread that has acquired a synchronized implicit lock calls object.wait () without arguments. Scenario 2: Calling thread.join () without arguments. Scenario 3: Call the locksupport.park () method. Call locksupport. park(Thread Thread) to wake up the target Thread, which transitions from WAITING to RUNNABLE.Copy the code

2.3 State Conversion between RUNNABLE and TIMED_WAITING

A. Call thread. sleep(long millis) with timeout parameter b. Call object. wait(long timeout) with timeout parameter c. Call thread.join (long millis) with timeout parameter d. Call the locksupport. parkNanos(Object Blocker, Long Deadline) method with the timeout parameter e. Call the locksupport.parkunitl (Long Deadline) method with the timeout parameterCopy the code

2.4 State conversion from NEW to RUNNABLE

Java's newly created Thread object is the NEW state, and there are two main ways to create a Thread object. 1) Inherit Thread objects and override the run() method.Copy the code

2) implements the Runnable interface, overriding the run() method and taking the implementation class as a parameter to create Thread objects.Copy the code

2.5 RUNNABLE to TERMINATED state

The thread automatically transitions to TERMINATED state after executing the run() method, and of course terminates if an exception is thrown during the run() method.