In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the state of Java threads". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the state of Java threads"?
1. Five states of a thread
At the operating system level, any thread generally has five states, namely, create, ready, run, block, and terminate.
(1) New status (NEW)
When a new thread, such as new Thread (), is created with a constructor in the program, the thread is in the creation state, and it already has the corresponding memory space and other resources, but has not yet started execution.
(2) ready state (READ)
After you create a new thread object, you can start the thread by calling the thread's start () method. When a thread starts, the thread enters a ready state (runnable)
Since the CPU has not yet been allocated, the thread will queue up to wait for the CPU service, indicating that it is ready to run. When the system picks a Thread object waiting for execution, it moves from the waiting state to the execution state. The action selected by the system is called "CPU scheduling". Once the CPU is obtained, the thread enters the running state and automatically invokes its own run method.
(3) running status (RUNNING)
When a thread in the ready state is called and gets the processor resources, the thread enters the running state. At this point, the run () method of the thread object is automatically called.
(4) blocking state (BLOCKED)
An executing thread will give up CPU and temporarily suspend its execution in some special cases, such as being artificially suspended or when it needs to perform time-consuming input and output operations.
In the executable state, if you call methods such as sleep (), suspend (), wait (), and so on, the thread will enter the blocking state. When blocking, the thread cannot enter the queue, and only when the cause of the blocking is eliminated, the thread enters the ready state and queues again, and then it is selected by CPU scheduling to continue execution from the original stop position.
Remember: after the blocking is eliminated, it returns to the ready state, not the running state.
(5) death status (TERMINATED)
After the thread calls stop (), destory () or run () execution ends, the thread is in a dead state. A thread in a dead state does not have the ability to continue to run.
2. 6 states of Java thread
The life cycle of a thread in Java is divided into six states. The Thread class has an instance property and an instance method dedicated to saving and fetching the state of a thread. The instance property used to save the state of the thread Thread instance is:
/ / Save the state of a thread as an integer private volatile int threadStatus = 0 handle / return the state of the current thread, an enumerated type value public State getState () {return sun.misc.VM.toThreadState (threadStatus);}
Thread.State is an internal enumeration class that defines six enumeration constants that represent six states of Java threads, as follows:
Public enum State {/ / New status NEW, / / running status RUNNABLE, / * * blocking status * Object.wait * / BLOCKED, / * * waiting status * Object.wait * Thread.join * LockSupport.park * / WAITING / * * time-limited waiting status * Thread.sleep * Object.wait * Thread.join * LockSupport.parkUntil * LockSupport.parkNanos * / TIMED_WAITING, / / termination status TERMINATED }
There are four common states: NEW state, RUNNABLE state, TERMINATED state, and TIMED_WAITING state.
(1) NEW status
The Java source code states that all thread instances created successfully but not started by calling the start () method are in the NEW state.
Of course, the state of the start () method that is not a Thread thread instance changes from the NEW state to the RUNNABLE state as soon as it is called, which does not mean that the thread immediately acquires the CPU time slice and executes it immediately, requiring a series of internal operations of the operating system.
(2) RUNNABLE status
When the start () method of the Thread instance is called, the next step is if the thread gets the CPU time slice to start execution, JVM will asynchronously call the thread's run () method to execute its business code. So what does JVM do before the run () method is called asynchronously? When the start () method of the Thread instance of the Java thread is called, the corresponding thread in the operating system enters the ready state instead of running state, while the Java thread does not have this ready state. What is the ready state of threads in the operating system? The transition relationship between the thread state of JVM and the thread state of the operating system behind it is simplified as shown in the figure:
If an operating system thread is in a ready state, it means that "everything is ready, except Dongfeng", that is, the thread has met the execution condition, but cannot be executed yet. The thread in the ready state needs to wait for the scheduling of the system. Once the ready state is selected by the system and obtains the CPU time slice, the thread begins to occupy CPU and begin to execute the thread's code. At this time, the operating system state of the thread changes and enters the running state.
In the operating system, a running thread returns to the ready state after running out of CPU time slices, waiting for the next scheduling of CPU. In this way, the operating system thread is repeatedly scheduled by the system between the ready state and the execution state, which continues until the logical execution of the thread's code completes or terminates abnormally. At this time, the operating system state of the thread changes again and enters the final state of the thread-the TERMINATED state.
Both the ready state and the running state are thread states in the operating system. In the Java language, instead of subdividing the two states, they are merged into the same state-- the RUNNABLE state. Therefore, in the Thread.State enumeration class, the ready state and running state of the thread are not defined, only the RUNNABLE state is defined. This is where the Java thread state differs from the thread state in the operating system.
In summary, after the Thread instance of NEW state calls the start () method, the state of the thread becomes RUNNABLE state. However, the thread's run () method is not necessarily executed concurrently immediately, and it needs to be started after the thread has obtained the CPU time slice.
(3) TERMINATED status
A thread in the RUNNABLE state becomes a terminated state TERMINATED after the execution of the run () method is complete. Of course, if a runtime exception occurs during the execution of the run () method without being caught, the run () method will be terminated abnormally and the thread will become a TERMINATED state.
(4) TIMED_WAITING status
The thread is in a special waiting state. To be exact, the thread is in a time-limited waiting state. There are roughly several operations that can put a thread in a time-limited wait state:
Thread.sleep (int n): puts the current thread into a time-limited wait state with a wait time of n milliseconds.
Object.wait (): monitor lock for preemptive objects with a time limit.
Thread.join (): thread merge with a time limit.
LockSupport.parkNanos (): lets the thread wait in nanoseconds.
LockSupport.parkUntil (): let the thread wait, and the time can be set flexibly.
3. Transition of Java thread state
Thank you for your reading, these are the contents of "what is the state of Java threads?" after the study of this article, I believe you have a deeper understanding of the state of Java threads, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.