Several states based on java thread (detailed explanation)

Threads can have six states:

  1. New (newly created)

  2. Runnable (run)

  3. Blocked

  4. Waiting

  5. Timed waiting

  6. Terminated

Newly created thread:

When you use the new operator to create a new thread, such as new thread (R), the thread has not started running. Its current state is new. There are still some basic work to be done before the thread runs.

Runnable thread:

Once the thread calls the start method, the thread is in the runnable state. The thread in this state may or may not be running (that is, the running thread in the Java specification is still in the runnable state). It also returns to the runnable state after the thread runs or returns from the blocking, waiting or sleep state.

Blocked thread:

In this state, it is a scenario where multiple threads have synchronous operations, such as waiting for the execution release of another thread's synchronized block, or someone else calls the wait () method in the reentrant synchronized block, that is, the thread is waiting to enter the critical area.

Waiting thread:

This state is that after the thread has a lock, it calls its wait method, or the thread calls the join method join another thread, waiting for the execution state ending by the thread of join. Here, we need to distinguish between blocking state and waiting state. One is to wait outside the critical point and the other is to wait inside the critical point.

Timed wait thread:

When a thread calls a method with a timeout parameter, it will cause the thread to enter the timing waiting state. The method with a supermarket parameter has thread sleep、Object. wait、Thread. join、Lock. Trylock and condition The timed version of await.

Terminated thread:

1) the thread dies naturally because the run method exits normally.

2) died unexpectedly because an uncapped exception terminated the run method.

Transitions between thread states:

The above several states based on Java threads (detailed explanation) are all the contents shared by Xiaobian. I hope to give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>