This is the 9th day of my participation in the August More Text Challenge. For details, see:August is more challenging
This article is from chaoCode. Please send a personal message and link to the author and the original address at the beginning of the article.
Violators, the author reserves the right to pursue.
preface
Today we will briefly talk about the start method of a thread.
If you are not familiar with how threads are created, it is recommended to watch Concurrent Programming — The 6 States and switches of Java Threads
Starting a thread
inConcurrent programming – Learn about threads in JavaWe talked about the creation of threads in The first section, and we start threads using the start method.
So let’s take a look at the start method and simply attach some source code:About threadStatus source code:ThreadStatus is a record of the state of a Thread. The initial Thread defaults to 0. We’re going to join the thread group, and we’re going to call start0(), and start0 is marked as native, which is a native method, which is a method written in another language, which is in the JDK code, and we don’t need to implement it or understand it.
Why is start0() marked as native?
It all starts with the fact that Java is cross-platform. Look at this diagram:
After the start() method calls the start0() method, the thread does not necessarily execute immediately, but it just puts the thread into a runnable state. The specific execution time depends on the CPU and is scheduled by the CPU in a unified manner.
We also know that Java is cross-platform and can run on different systems, and each system has a different CPU scheduling algorithm, so we need to do different processing, this thing can only be implemented by the JVM, start0() method naturally made the table markup native.
Start () method cannot be used for many times by just source code analysis, knew that the start method at first and then check the thread state, when a thread has been created or over, the state is different from that of initialization state will be thrown IllegalThreadStateException anomalies.
Note: The start method is a method modified by synchronized, which ensures thread safety. The main method thread and system group thread created by the JVM are not started with start.
This is where the interviewer will ask you what the difference is between the run method inside the Thread and the start() provided by the Thread. Why use the start() method to start a thread?
The run method: It’s just an ordinary method in Runnable object, the common method of direct call is equivalent to the calling we, does not create new threads in the program, or is it just a main thread, code execution or in accordance with the order, which is equivalent to wait for the run method is performed, so using the run method is not the way to call setup thread.
Start method: start the thread with the start method, the real implementation of multithreaded run, at this time do not need to wait for the body of the run method code to complete and directly continue to execute the following code. Start a Thread by calling the start() method of Thread class. The Thread is in the ready (runable) state and is not running. Once the CPU slice is obtained, the run() method is called the body of the Thread and contains the contents of the Thread to be executed. The thread then terminates.
Thank you for watching, and feel free to let us know in the comments section if you have any mistakes. If this post has helped you, feel free to like it at 👍.