Operation skills of java thread start method callback run method

During the interview, you may be asked why we execute the run () method when we call the start () method, and why we can't call the run () method directly?

Java thread creation method

In fact, the most important thing to create a thread is to provide a thread function (callback function), which is used as the entry function of the newly created thread to realize the functions you want. Java provides two methods to create a thread:

Inherit thread class

Implement the run method of the inherited class, and then create the object of this subclass. Call the start method to create a new thread:

Implement runnable interface

The object of the class implementing the runnable interface can be passed as a parameter to the created thread object. Similarly, calling the thread#start method can run the code in the run method in a new thread.

You can see that no matter which method you use, you actually need to implement a run method. This method is essentially a callback method. The thread newly created by the start method will call this method to execute the required code. As you can see later, the run method is not a real thread function, but a Java method called by the thread function, which is not fundamentally different from other Java methods.

Implementation of java thread

Conceptually, The creation of a java thread basically corresponds to a local thread The creation of native thread is one-to-one correspondence. The problem is that the local thread should execute local code, and the thread function provided by java thread is Java method and compiled Java bytecode. Therefore, it can be imagined that java thread actually provides a unified thread function, which calls java thread method through Java virtual machine, This is achieved through Java Native method calls.

The following is an example of the thread#start method:

You can see that it actually calls the local method start0. The declaration of this method is as follows:

Thread class has a registernatives local method. The main function of this method is to register some local methods for thread class, such as start0(), stop0(), etc. it can be said that all local methods operating local threads are registered by it This method is placed in a static statement block, which means that when the class is loaded into the JVM, it will be called to register the corresponding local method.

The local method registernatives is defined in thread C in the document. Thread. C is a small file that defines the common data and operations about threads used by each operating system platform, as shown in code listing 1.

Listing 1

At this point, it is easy to see that when a java thread calls the start method, it will actually call the JVM_ Startthread method, what is the logic of this method. In fact, what we need (or Java expression behavior) is that this method will eventually call the run method of the java thread, which is indeed the case. In jvm.cpp, there is the following code segment:

**Here is the JVM_ Entry is a macro used to define * * JVM_ In the startthread function, you can see that a real platform related local thread is created in the function, and its thread function is thread_ Entry, as shown in Listing 2.

Listing 2

You can see that vmsymbolhandles:: run is called_ method_ Name method, which is in vmsymbols HPP macro defined:

As for run_ method_ How name is declared and defined is not covered in this article because it involves very cumbersome code details. Interested readers can view the source code of the JVM by themselves.

Fig Create call graph with java thread

Start() creates a new process. Run() does not

PS: let's look at the difference between run and start methods in Java threads

The difference between run () and start () methods in thread class is as follows:

Run () method: call the run () method of the runnable object in this thread, which can be called repeatedly;

Start () method: start a thread and call the run () method of the runnable object. You cannot start a thread multiple times;

summary

The above is the operation skills of java thread start method and callback run method introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

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