当前位置:网站首页>Call analysis of start method in JNI thread and callback analysis of run method

Call analysis of start method in JNI thread and callback analysis of run method

2020-11-06 21:07:00 itread01

### ** Preface ** stay java In programming , Thread Thread It's a class that we often use . So create a Thread What is the essence of the , This paper makes an exploration on this problem . The content is mainly divided into the following parts 1.JNI The use of mechanisms 2.Thread Create the underlying call analysis of the thread 3. The use of system threads 4.Thread in run Call back analysis of methods 5. Implement a jni Call back of ## **1.JNI The basic use of mechanisms ** When we new Give one Thread When , It's just building a java Thread objects at the level of , And only when Thread Of start When the method is called , A thread really starts to execute . therefore start The method is what we focus on Look at Thread Class start Method ```java public synchronized void start() { if (threadStatus != 0) throw new IllegalThreadStateException(); group.add(this); boolean started = false; try { start0(); started = true; } finally { try { if (!started) { group.threadStartFailed(this); } } catch (Throwable ignore) { } } } ``` Start The method itself is not complicated , Its core is start0(), Actually start the thread . Then we examine start0() Method ```java private native void start0(); ``` You can see that this is a native Method , Here we need to explain what is native Method . As we all know java It's a cross platform language , use java Compiled code can be run on any installation jvm On the system of . However, the underlying implementation of each system must be different , In order to java Can cross platform , On jvm It's called java native interface(JNI) Mechanism of . When java When you need to use some system approach , from jvm Help us call the bottom of the system , and java You just need to tell jvm What needs to be done , Call someone native The method can . for example , When we need to start a thread , No matter on which platform , All we're calling for is start0 Method , from jvm According to different operating systems , Call the underlying method of the corresponding system , Help us actually start a thread . So it's like jvm It provides an interface for us to operate the underlying methods of the operating system , namely JNI,java Local interfaces . In depth examination start0() Before method , Let's make our own JNI Method , Only in this way can we better understand start0() How the method is called to the system level native Method . First of all, let's define a simple java Class ```java package cn.tera.jni; public class JniTest { public native void jniHello(); public static void main(String[] args) { JniTest jni = new JniTest(); jni.jniHello(); } } ``` In this class , We defined a jniHello Of native Method , And then in main Method to call it . Then we called javac Command to compile it into a class Archives , But it's different from usual , We need to add a -h Arguments , Generate a header file ``` javac -h . JniTest.java ``` Be careful -h There's one in the back ., It means the generated header file , Stored in the current directory At this point, we can see that in the current directory 2 A new file **JniTest.class**:JniTest Class bit group code **cn_tera_jni_JniTest.h**:.h Header file , This file is C and C++ What you need to use in , The argument of the method is defined 、 Return type, etc , But it doesn't include implementation , Similar java Interface in , and java This is where the code goes “ Interface ” Find the method that really needs to be executed . Let's examine this .h Archives , That's included jniHello Definition of method , Of course, it should be noted that , Here and the name of the method .h The name of the file itself is jni According to the package name and class name of our class , Do not modify . ```c /* DO NOT EDIT THIS FILE - it is machine generated */ #

版权声明
本文为[itread01]所创,转载请带上原文链接,感谢