Java thread status and transition relationship

Thread status

Before understanding the Java thread conversion relationship, we need to first determine the status of the Java thread. There are many sayings on the Internet that there are 5 states, but they are actually not correct. Find the anonymous internal enumeration class java.lang.Thread.State by looking inside the Thread class, which contains the following six enumerations:

NEW initial

The status of threads that have not yet been started. That is, the thread object is created, but the thread's start() method has not been called.

RUNNABLE run

The thread state of the runnable thread. A thread in a runnable state executes within the Java virtual machine, but it may be waiting for other resources from the operating system, such as a processor. In other words, the two states of ready and running in Java threads are generally called "running". The thread in this state is located in the runnable thread pool, waiting to be selected by thread scheduling and obtain the right to use the CPU. At this time, it is in the ready state. The thread in the ready state becomes running after obtaining the CPU time slice.

BLOCKED blocked

A blocked thread is waiting for a lock to enter a synchronized block/method, or to re-enter a synchronized block/method after calling a synchronized block/method.

WAITING Waiting

The status of a waiting thread. A thread is in a waiting state due to a call to one of the following methods:

Object.wait() //无限等待
Thread.join() //无限等待
LockSupport.park()

 A thread in the wait state is waiting for another thread to perform a specific action. For example, a thread that called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on the object. A thread that called Thread.join() is waiting for a specified thread to terminate.

TIMED_WAITING Timeout waiting

The thread state of a waiting thread with a specified wait time. A thread is in a timed wait state by calling one of the following methods with a specified positive wait time:

Thread.sleep(long)
Object.wait(long)
Thread.join(long)
LockSupport.parkNanos()
LockSupport.parkUntil()

TERMINATED

The status of the terminated thread. The thread has completed execution.

conversion relationship

I saw a picture in another blog and the description is relatively accurate. But we generally think that the thread enters the ready state first, and then the running state. So the picture is modified to the following picture:

 

 

 

Guess you like

Origin blog.csdn.net/qq_20051535/article/details/120249577