Home >Java >javaTutorial >do you know? Threads in java actually have 6 states
First of all, let me tell you that the 5 states mentioned on the Internet are misleading. Threads in Java actually have 6 states. The term "5 states" is actually the state of the early process.
The status of the early process:
The so-called "processstatus" here refers to the early "single-threadedprocess" status.
For the now common "multi-threaded process", obviously, it is meaningless to talk about "process status". We should talk about "the status of a thread under the process" or directly "thread status". However, sometimes "process status" and "thread status" are still confused.
Some systems call threads "light-weight processes", so we are still talking about "process status".
Sometimes they are not even called "process" or "thread", they are called "task" or "job".
The difference with the traditional running state
Some people often think that there is a running state missing from the Java thread state. This is actually confusing two different levels of states. . For the Java thread state, there is no so-called running state, and its runnable state includes the running state.
We may ask, why is there no distinction between these two states in the JVM?
The current Time-sharing(time-sharing)Multi-task(multi-task) operating system architecture usually uses the so-called "Time Sharing (time quantum or time slice)" method is used to perform preemptive (preemptive) round-robin scheduling (round-robin type).
This time slice is usually very small. A thread can only run on the CPU for a maximum of 10-20ms at a time (it is in the running state), which is only about 0.01 seconds. , after the time slice is used, it will be switched down and placed at the end of the scheduling queue to wait for scheduling again. (That is, return to the ready state)
Usually, Java's thread status is for monitoring. If the thread switches so quickly, then it doesn't make much sense to distinguish between ready and running.
Today’s mainstream JVM implementations map Java threads one by one to the underlying threads of the operating system, and entrust the scheduling to the operating system. The state we see at the virtual machine level is essentially a mapping of the underlying state. and packaging. The JVM itself does not do any substantial scheduling, and it does not make much sense to map the underlying ready and running states. Therefore, it is a good choice to unify them into runnable states.
The following are the comments in the Thread.State source code:
These states are virtual machine states which do not reflect any operating system thread states.
These states are virtual machine states which do not reflect any operating system thread states.
Comments on RUNABLE state:
A thread in the runnable state is executing in the Java virtual machine but it may be waiting for other resources from the operating system such as processor.
The thread in the runnable state is executing in the Java virtual machine, but it may be waiting for other resources from the operating system, such as the processor.
The RUNNABLE state in Java actually includes the Ready and Running states, so you can completely ignore the inaccurate statements on the Internet. The answer to such questions is often in the source code and javadoc.
6 status:
NEW (new)
RUNNABLE (runnable)
BLOCKED (blocked)
WAITING (infinite waiting)
TIMED_WAITING (limited time waiting)
TERMINATED(TERMINATION)
Related articles:
Java Example-Getting Thread Status
Sharing of Several Statuses in Java Threads
The above is the detailed content of do you know? Threads in java actually have 6 states. For more information, please follow other related articles on the PHP Chinese website!