About the running status of Java threads

The first thing to note is that the referenced state is the JVM thread state, not the operating system thread state. At the same time, a thread will only exist in one state.

Thread state, enum State:

1、NEW

Has been created and is not running.

2、RUNNABLE

The thread is running in the JVM.

3、BLOCKED

Block waiting for monitor lock to enter or re-enter synchronized synchronized block or method, triggered by Object.wait.

4、WAITING

Trigger methods: Object.wait, join (), LockSupport.park

Wait for another thread to finish a specific task.

For example: a
thread executes Object.wait, waits for another thread to finish execution, and wakes up by executing Object.notify or Object.notifyAll to continue execution;

The thread executes Thread.join, waiting for the completion of the execution of a specific thread.

5、TIMED_WAITING

Waiting for the time limit.

触发方法:Thread.sleep、Object.wait(timeout)、Thread.join(timeout)、LockSupport.parkNanos、LockSupport.parkUntil。

6、TERMINATED

The thread status of the completed task.

 

Note:

1. The thread Thread.getState method gets the current thread state. This method is used to monitor the status of the system, not to synchronize control.

2. The Unsafe.park method suspends the thread.

3. The Thread.join method internally calls the Object.wait method to achieve waiting.

4. The BLOCKED state emphasizes that it is waiting to enter the synchronization logic.

 

Guess you like

Origin www.cnblogs.com/niejunlei/p/12731788.html