Java: six states and transformations of threads

Java: six states and transformations of threads

There are different opinions on thread life cycle in online books, which is difficult to unify. This article makes a summary:

java. lang.Thread. Six thread states are defined in the state enumeration class. You can call the getstate () method in the thread to obtain the current thread state.

The following figure is from Figure 4-1 of the art of Java Concurrent Programming

1、 New status (New)

That is, create a new thread with the new keyword, and the thread is in the new state.

2、 Running status (runnable)

The ready and running states in the operating system are collectively referred to as runnable in Java.

Ready status (ready)

When the thread object calls the start () method, the thread is in a ready state. Ready means that the thread can execute, but the specific execution time will depend on the scheduling of the thread scheduler in the JVM.

Other status - > ready

Running status

After the thread in the ready state obtains the CPU, when it really starts executing the thread execution body of the run () method, it means that the thread is already in the running state. It should be noted that for a single processor, only one thread can be running at a time. For a preemptive system, each thread will be given a short period of time to process its own tasks. After the time runs out, the system is responsible for recapturing the resources occupied by the thread. In the next period of time, the system will schedule again according to certain rules.

When the running state changes to ready state:

Official interpretation of yield method:

Prompt the scheduler that the current thread is willing to give up the current use of the processor. At this time, the current thread will be set to the ready state and wait for scheduling like other threads. At this time, according to the probability determined by different priorities, the current thread is completely possible to grab processor resources again.

3、 Blocked status

A blocking state indicates a state in which a thread is waiting for a monitor lock.

The following scenario threads will block:

When a thread acquires a lock, it changes from a blocked state to a ready state.

4、 Waiting status

Entering this state indicates that the current thread needs to wait for other threads to make some specific actions (notifications or interrupts).

Run - > wait

Wait - > ready

5、 Timed_waiting

Different from waiting, it can return by itself at a specified time.

Run - > timeout wait

Supplement: the difference between sleep and Yield:

Timeout - > ready

6、 Extinction state

That is, the termination of the thread indicates that the thread has been executed. As mentioned earlier, a dead thread cannot be awakened again through start.

It should be noted that the main thread and the sub thread do not affect each other, and the sub thread will not end because the main thread ends.

Many places still need to be supplemented later. Please look forward to it.

References: Java Concurrent Programming practice, crazy Java handout, Java Concurrent Programming Art

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
分享
二维码
< <上一篇
下一篇>>