[JVM] local method interface

1, Local method

What is a local method?

  1. In short, a Native Method is an interface for Java to call non java code

  2. A Native Method is a Java method that is implemented by a non Java language, such as C.

  3. This feature is not unique to Java. Many other programming languages have this mechanism. For example, in C + +, you can use extern to tell the C + + compiler to call a C function.

  4. “A native method is a Java method whose implementation is provided by non-java code.” (local method is a non Java method, and its specific implementation is the implementation of non java code)

  5. When defining a native method, it does not provide an implementation (some like defining a Java interface), because its implementation is implemented outside by a non Java language.

  6. The function of local interface is to integrate different programming languages for Java. Its original intention is to integrate C/C + + programs

  

Examples of native methods

getClass() method of Get Object class

1 public final native Class<?> getClass();

The start() method of the Thread class calls the start0() method

 1 public synchronized void start() {
 2     /**
 3      * This method is not invoked for the main method thread or "system"
 4      * group threads created/set up by the VM. Any new functionality added
 5      * to this method in the future may have to also be added to the VM.
 6      *
 7      * A zero status value corresponds to state "NEW".
 8      */
 9     if (threadStatus != 0)
10         throw new IllegalThreadStateException();
11 
12     /* Notify the group that this thread is about to be started
13      * so that it can be added to the group's list of threads
14      * and the group's unstarted count can be decremented. */
15     group.add(this);
16 
17     boolean started = false;
18     try {
19         start0();
20         started = true;
21     } finally {
22         try {
23             if (!started) {
24                 group.threadStartFailed(this);
25             }
26         } catch (Throwable ignore) {
27             /* do nothing. If start0 threw a Throwable then
28               it will be passed up the call stack */
29         }
30     }
31 }
32 
33 private native void start0();  
  • The code illustrates how the Native method is written

  • It should be noted that the identifier native can be used with other java identifiers, except abstract

 1 public class IHaveNatives {
 2 
 3     public native void methodNative1(Object x);
 4 
 5     public native static long methodNative2();
 6 
 7     private native synchronized float methodNative3(Object o);
 8 
 9     native void methodNative4(int[] ary) throws Exception;
10 
11 }

Why use Native Method?

Java is very convenient to use. However, it is not easy to implement some level tasks in Java, or we are very concerned about the efficiency of the program.

Interaction with Java environment

  1. Sometimes Java applications need to interact with the hardware environment outside Java, which is the main reason for the existence of local methods. You can think about the situation when Java needs to exchange information with some underlying systems, such as the operating system or some hardware.

  2. Local method is such a communication mechanism: it provides us with a very concise interface, and we don't need to understand the cumbersome details outside Java applications.

Interaction with operating system

  1. JVM supports the Java language itself and runtime library. It is the platform on which Java programs depend. It is composed of an interpreter (Interpreting bytecode) and some libraries connected to local code.

  2. However, it is not a complete system after all. It often depends on the support of an underlying system. These underlying systems are often powerful operating systems.

  3. By using local methods, we can realize the interaction between jre and the underlying system in Java, and even some parts of the JVM are written in C.

  4. Also, if we want to use some features of the operating system that the Java language itself does not provide encapsulation, we also need to use local methods.

  Sun's Java

  1. Sun's interpreter is implemented in C, which enables it to interact with the outside world like some ordinary C. jre is mostly implemented in Java, and it also interacts with the outside world through some local methods.

  2. For example: class java The setPriority() method of lang. thread is implemented in Java, but it calls the local method setPriority0() in this class. This local method is implemented in C and embedded in the JVM. On the platform of Windows 95, this local method will eventually call the Win32 setpriority() API.

  3. This is the specific implementation of a local method, which is directly provided by the JVM. More often, the local method is provided by an external dynamic link library and then called by the JVM.

Current status of local methods

At present, this method is used less and less, except for hardware related applications, such as driving printers through Java programs or managing production equipment through Java systems, which are rare in enterprise applications. Because now the communication between heterogeneous fields is very developed. For example, Socket communication or Web Service can be used. I won't introduce more.

  

Tags: Java jvm

Posted by raouleduke on Mon, 02 May 2022 06:21:01 +0300