This article is reprinted from the WeChat public account "Program Cat Adult", the author is Program Cat Adult. Please contact Program Cat Adult's public account to reprint this article. Differences between Java threads and Native (OS) threadsConnection: Java threads are actually an encapsulation of an OS thread, and are essentially OS threads. [Previous versions of Java threads are not OS threads, but user-mode threads (Green Threads) constructed by the JVM, which cannot fully utilize the CPU. Later, they have been changed to use OS threads. ] [Reference https://mp.weixin.qq.com/s/Gxqnf5vjyaI8eSYejm7zeQ] the difference: Java threads can directly get JNIEnv, while OS threads need to attach to JVM before they can get JNIEnv. [My personal understanding is that the difference lies in whether JVM is attached]
The Java thread can FindClass successfully, but the OS thread fails to FindClass because the ClassLoaders of the two are different. The ClassLoader held by the OS thread after AttachCurrentThread is the system's ClassLoader. If you want to FindClass successfully, you need to obtain a copy of the current library's ClassLoader during JNI_Onload and save it, and use this ClassLoader to operate the next time you FindClass.
What does JNI do?Here is a passage from the official document https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html#wp16696 translated by someone else: The most important design goal of JNI is to provide binary compatibility between JVMs on different operating systems, so that a native library can run on JVMs of different systems without recompiling. To achieve this, JNI cannot care about the internal implementation of JVM when designing, because the internal implementation mechanism of JVM is constantly changing, and we must keep the JNI interface stable. The second design goal of JNI is efficiency. We may see that sometimes we may need to sacrifice a little efficiency to meet the first goal, so we need to make some choices between platform independence and efficiency. Finally, JNI must be a complete system. It must provide enough JVM functions for native programs to complete some useful tasks. JNI cannot only target a specific JVM, but also provide a series of standard interfaces so that programmers can load their native code libraries into different JVMs. Sometimes, calling the interface implemented under a specific JVM can provide efficiency, but in more cases, we need to use more general interfaces to solve the problem. JNIEnv and JavaVMIt's a function pointer. The following figure is the pointer structure of JNIEnv: JNIEnv is actually an interface pointer pointing to local thread data, which contains a pointer to the function interface. Each interface function has a predefined offset position in this table, similar to the C++ virtual function table. The code is as follows:
Knowledge point 1: Why use function tables instead of hard-coding certain function items? The JNI namespace can be separated from the native code, and a virtual machine can provide multiple versions of JNI function tables for different scenarios. For example, a virtual machine can support two types of JNI function tables: One is for debugging, doing more error checking. One for publishing, which does less error checking and is more efficient. Knowledge point 2: JNIEnv is thread-local, which means it is only valid in the current thread. Native methods cannot pass JNIenv from the current thread to another thread. JNIEnv cannot be used across threads [as for why JNIEnv is designed to be thread-local, I don't understand]. Knowledge point 3: Although threads do not share JNIEnv, they share JavaVM, and then the JNIEnv of the current thread can be obtained through GetEnv. jint GetEnv(JavaVM *vm, void **env, jint version); Knowledge point 4: Native methods receive JNI interface pointers as parameters. The virtual machine ensures that the same JNIEnv is passed to Native methods in the same thread. If different threads call Native methods, the JNIEnv passed to them is different. However, the function table indirectly pointed to by JNIEnv is shared among multiple threads. Knowledge point 5: Why does calling a native method in C language require passing JNIEnv as a parameter, but not in C++?
The JNIEnv listed above is in C language form. Java also encapsulates a layer of JNIEnv for C++. The simplified version of the code is:
In fact, it is essentially an interface in the form of a C language call. How to pass data in JNII won't go into detail here, but basically, basic types like int and float are copied, while objects and byte arrays use references. So it actually takes little time to transfer byte stream data from the Java layer to the Native layer, and no copying will occur [but if the Native layer wants to use and hold this data, it has to make a copy itself]. There are also some knowledge points such as GlobalReference, LocalReference and why to Delete LocalReference. These are relatively basic and will not be introduced here. I guess everyone understands them. Recommended Reading https://www.cnblogs.com/kexinxin/p/11689641.html NDK official documentation https://developer.android.com/ndk/guides References http://luori366.github.io/JNI_doc/jni_design_theory.html https://www.cnblogs.com/kexinxin/p/11689641.html https://developer.android.com/ndk/guides |
<<: Detailed explanation of the use and properties of three types of animations in App development
Before the Spring Festival, I met with the market...
In 2019, the concept of KOC (Key Opinion Consumer...
Xiaohongshu needs to break out of its niche. For ...
Review expert: Wang Xuejiang, professor at Capita...
January 4 (Reporter Zhang Qian) Recently, Alcatel...
There are unknown secrets behind every screen-swe...
Introduction: In today's mobile era, short vi...
39 sideline money-making projects to teach you ho...
Get 5000+ customers at 0 cost, how does Daxiex at...
Produced by: Science Popularization China Author:...
Produced by: Science Popularization China Produce...
November 22 was supposed to be the first birthday...
Nowadays, everything advocates refined operations...
He Jia CEO Speech Coach • Tutored the CEO class o...
Recently, documents from the California Departmen...