By Thread.sleep to understand JNI

What is JNI

I heard the JNI -> want to learn -> to do their own experiments and found that fast -> want in-depth study -> Google -> Articles found only a little -> read, full of rubbish -> find the official text / burst Stack / on know almost question -> stepped on his own pit ->
way to learn a bunch of additional skills, such as Rust / custom JVM, etc. -> write tutorials, find too much content, and lazy -> give up writing a tutorial, to a wiki -> stiff - scalp write it! (Behind my italics)

Java language and environment for application development is very safe and efficient. However, some applications they need to perform some tasks can not be completed pure Java program, such as:

  1. And legacy code integration, to avoid re-writing.
  2. Implementation is available in the library of the missing functionality. For example, when implementing ping in the Java language, you may need Internet Control Message Protocol (ICMP) function, but the basic class library does not provide it.
  3. The best written using C / C ++ code integration, in order to fully exploit the performance or other environment-related system characteristics.
  4. Address the special circumstances require non-Java code. For example, to achieve the core libraries may need to call or cross-package need to bypass other Java security checks.

With JNI, native code can interact with Java objects, access and design field values, and call methods at will, and not as subject to many limitations like the same functions in Java code.
This freedom is a double-edged sword: it sacrifices the security of Java code, in exchange for the ability to complete the tasks listed above. Use JNI provides a powerful, machine resources in your application in
low-level access (memory, I / O, etc.), so you will not be protected as a safety net like ordinary Java developers. JNI's flexibility and power of some risks on programming practices,
such as lead to poor performance, bug occurs even crash. You need to be careful code in the application, and use good practices to protect the overall integrity of the application.
JVM in a native method when the link will be to find the underlying implementation JNI function based naming method, this specification is C linkage

JNI Programming items

JNI programming pitfalls can be divided into two categories:

Performance: The code can perform the designed function, but run slowly or slow down the entire process in some form.

  • No caching method ID, or ID and class field
  • Trigger copy of the array
  • Returning (Reaching back) instead of passing parameters
  • Error found the line between native code and java code
  • Extensive use of local references without informing the JVM

Correctness: Code sometimes work fine, but not reliably provide the required functionality; worst case, cause the program to crash or hang.

  • Using the wrong JNIEnv
  • To detect abnormalities
  • The return value for the detection of
  • To properly use the array method
  • Global reference for the proper use

Thread.sleep come by understanding JNI

public static native void sleep(long millis) throws InterruptedException;

This is the Thread methods of sleep function, we can see that he is a native method, according to our understanding of navite keywords, think this is a local method, then what is the local way to do that?
I personally think that: a method of non-java language to achieve. The JNI is responsible for calling each other's methods and non-java java method

Next we went to sleep corresponding local implementation, I use OpenJDK7, you can download it and see, openjdk \ jdk \ src \ share directory is to achieve a platform-independent code,
then we can find openjdk \ jdk \ src \ share \ native \ java \ lang \ Thread.c open but we did not find implementation code found

Let me talk about this structure JNINativeMethod

Next we use for JVM_Sleep Sublime keyword search, you can find openjdk \ hotspot \ src \ share \ vm \ prims \ jvm.cpp have achieved the JVM_Sleep

The process is very simple, say a few main parameters

JVM_ENTRY JVM_END are in openjdk \ hotspot \ src \ share vm \ runtime \ interfaceSupport.hpp which defines \ a macro

JVM_ENTRY is a preprocessor macro that adds some boilerplate code that is common for all functions of HotSpot JVM API. This API is a connection layer between the native code of JDK class library and the JVM.
What JVM_ENTRY macro does:
Gets the pointer to current JavaThread from JNIEnv* argument.
Performs thread state transition from _thread_in_native to _thread_in_vm, blocking at safepoint if necessary.. 
Cleans JNI local references on the exit from the function. 
Takes care of debug tracing and verification in debug builds of JVM.

vmSymbols: openjdk \ hotspot \ src \ share \ vm \ classfile \ vmSymbols.hpp which defines the various mapping of java

HS_DTRACE_PROBE1: HotSpot dynamic tracking probe (Buried)

ConvertSleepToYield: sleep turn yield. yield will release CPU resources to higher priority (at least the same) execution threads to get the chance; you can see from the code when sleep incoming 0 if converted both yield the same success. Otherwise he would sleepMinSleepInterval time slice

Process: first get the current state of the thread, and then set SLEEPING, performed next os: sleep method, if the process occurs, the exception will be thrown. State before then threads to the finish after sleep

JDK11 the JNI specification

Guess you like

Origin www.cnblogs.com/colin-xun/p/11772880.html