In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains why Java multithreading startup calls the start () method instead of the run () method. The content of 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 why Java multithread startup calls the start () method instead of the run () method.
The state of the thread
In Java, six thread states are defined, which can be found in the Thread class:
/ / to save space, I deleted the comment public enum State {NEW,// initial state RUNNABLE,// running state BLOCKED,// blocking state WAITING,// waiting state TIMED_WAITING,// timeout waiting state TERMINATED;// termination state}
For the correlation between these six states, you can see the following figure:
This picture describes in great detail, combined with this picture, let's talk about what these states mean respectively:
1. NEW indicates that the thread was created successfully, but did not run. After new Thread and before start, the thread is in NEW state.
2. RUNNABLE indicates that the thread is running. When we run the strat method and the child thread is created successfully, the state of the child thread becomes RUNNABLE.
3. TERMINATED means that the thread has finished running, and the child thread has completed, interrupted or aborted, and the state will change from RUNNABLE to TERMINATED.
4. BLOCKED means that the thread is blocked. If the thread happens to be waiting to acquire the monitor lock lock, for example, it will change from RUNNABLE to BLOCKED when waiting to enter a synchronized-decorated code block or method.
5. Both WAITING and TIMED_WAITING mean waiting. Now when it encounters methods such as Object#wait, Thread#join, and LockSupport#park, the thread will wait for another thread to perform a specific action before it can wait in bundles, except that TIMED_WAITING has a waiting time.
Priority
The priority represents the opportunity for the thread to execute. The high priority may be executed first and the low may be executed later.
In the Java source code, the priority is 1 to 10 from low to high, and the default new priority of the thread is 5. The source code is as follows:
/ * The minimum priority that a thread can have. * / public final static int MIN_PRIORITY = 1; / * * The default priority that is assigned to a thread. * / public final static int NORM_PRIORITY = 5; / * * The maximum priority that a thread can have. * / public final static int MAX_PRIORITY = 10
How threads are created
There are two ways to create multithreading, one is to inherit the Thread class, and the other is to implement the Runnable interface. The use of the two methods is as follows:
1. Inherit Thread and become a subclass of Thread
Public class MyThread extends Thread {@ Override public void run () {System.out.println ("I am implemented by inheriting the Thread class ~");} public static void main (String [] args) {MyThread thread = new MyThread (); / / start thread thread.start ();}}
2. Implement Runnable interface
Public class MyThread1 {public static void main (String [] args) {Thread thread = new Thread (new Runnable () {@ Override public void run () {System.out.println ("I am implemented through runnable ~";}}); / / start thread thread.start ();}}
No matter which way you use, starting a thread is the thread.start () method. If you have done an experiment, you will find that thread.run () can also be executed, so why do you have to call the thread.start () method?
First of all, let's talk about the conclusion: first, the method can be executed through the object .run () method, but it is either a multithreaded way or a common method. If you want to achieve the multithreaded way, you must use the object .start () method.
A better way to understand a problem is to start with the source code. Let's start with the source code of these two methods. Let's first take a look at the source code of the start method:
Public synchronized void start () {/ * This method is not invoked for the main method thread or "system" * group threads created/set up by the VM. Any new functionality added * to this method in the future may have to also be added to the VM. * A zero status value corresponds to state "NEW". * / without initialization, throw an exception if (threadStatus! = 0) throw new IllegalThreadStateException (); / * Notify the group that this thread is about to be started * so that it can be added to the group's list of threads * and the group's unstarted count can be decremented. * / group.add (this); / / the identifier of whether or not to start boolean started = false; try {/ / start0 () is the key to starting multithreading / / A new thread is created here, which is a native method / / after the execution is completed, the new thread is already running start0 (); / / the main thread executes started = true } finally {try {if (! started) {group.threadStartFailed (this);}} catch (Throwable ignore) {/ * do nothing. If start0 threw a Throwable then it will be passed up the call stack * /}
The source code of the start method is only a few lines of code, and the comments are more detailed, the most important of which is the start0 () method, which will be explained later. Let's look at the source code of the run () method:
@ Override public void run () {/ / simply runs without starting a new thread. Target is Runnable if (target! = null) {target.run ();}}
The source code of the run () method is relatively simple, which is a call to a common method, which confirms our above conclusion.
Next, let's talk about the start0 () method, which is the key to the real implementation of multithreading. The start0 () code is as follows:
Private native void start0 ()
Start0 is marked as native, that is, the local method, and we do not need to implement or understand, * * Why is start0 () marked as native * *?
This starts with Java cross-platform. Take a look at the following picture:
After the start () method calls the start0 () method, the thread doesn't necessarily execute immediately, it just makes the thread runnable (NEW-- > RUNNABLE). The exact time to execute depends on the CPU, which is uniformly dispatched by CPU.
We also know that Java is cross-platform and can be run on different systems, and the CPU scheduling algorithm for each system is different, so we need to do different processing, so this can only be done by JVM, and the start0 () method is naturally marked as native.
Finally, to sum up, the real multithreading implementation in Java is the start0 () method in start, and the run () method is just a common method.
Thank you for your reading, the above is the "Java multithreaded startup why the call is start () method rather than run () method" of the content, after the study of this article, I believe that Java multithreaded startup why the call is start () method rather than run () method this question has a deeper understanding, the specific use of the need for you to practice verification. 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.