5. Local method interface and local method stack

5.1. What are native methods?

Simply put, a Native Method is an interface for Java to call non-Java code. A Native Method is a Java method whose implementation is implemented in a non-Java language, such as C. This feature is not unique to Java. Many other programming languages ​​have this mechanism. For example, in C, you can use extern "c" to tell the c compiler to call a c function.

A native method is a Java method whose implementation is provided by non-java code.

When defining a native method, the implementation body is not provided (somewhat like defining a Java interface), because its implementation body is implemented externally by non-Java languages.

The function of the local interface is to integrate different programming languages ​​​​for Java. Its original intention is to integrate C/C++ programs.
Insert image description here

public class IHaveNatives{
    
    
    public native void methodNative1(int x);
    public native static long methodNative2();
    private native synchronized float methodNative3(Object o);
    native void methodNative4(int[] ary) throws Exception;
}

The identifier native can be used with other java identifiers, except abstract

5.2. Why use Native Method?

Java is very convenient to use, but some levels of tasks are not easy to implement in Java, or when we are very concerned about the efficiency of the program, problems arise.

Interaction with the Java environment

Sometimes Java applications need to interact with the environment outside Java, which is the main reason for the existence of native methods . You can think about the situation when Java needs to exchange information with some underlying system, such as the operating system or some hardware. Local methods are exactly such a communication mechanism: it provides us with a very simple interface, and we don't need to understand the cumbersome details outside of the Java application.

Interaction with the operating system

The JVM supports the Java language itself and the runtime library. It is the platform on which Java programs rely. It consists of an interpreter (interpreting bytecode) and some libraries connected to native code. However, it is not a complete system after all, and it often relies on the support of an underlying system. These underlying systems are often powerful operating systems. By using native methods, we can use Java to implement jre's interaction with the underlying system, and even some parts of the JVM are written in c . Also, if we want to use some operating system features that the Java language itself does not provide encapsulation, we also need to use native methods.

Sun’s Java

Sun's interpreter is implemented in C, which allows it to interact with the outside world like some ordinary C. Most of jre is implemented in Java, and it also interacts with the outside world through some native methods. For example: the setPriority() method of class java.lang.Thread is implemented in Java, but it calls the local method setPriority() in the class. This native method is implemented in C and implanted inside the JVM. On the Windows 95 platform, this native method will eventually call the Win32 setPriority() API. This is a specific implementation of a local method provided directly by the JVM. More often, the local method is provided by an external dynamic link library (external dynamic link library) and then called by JVw.

status quo

At present, this method is used less and less, unless it is a hardware-related application, such as driving a printer through a Java program or managing production equipment through a Java system, which is relatively rare in enterprise-level applications. Because the communication between heterogeneous fields is now very developed, for example, you can use Socket communication, you can also use Web Service, etc., I won’t introduce it in detail.

5.2. Local method stack

The Java virtual machine stack is used to manage the invocation of Java methods, and the local method stack is used to manage the invocation of local methods.

The local method stack is also thread-private.

Allows to be implemented with a fixed or dynamically expandable memory size. (same in terms of memory overflow)

● If the stack capacity requested by the thread exceeds the maximum capacity allowed by the native method stack, the Java virtual machine will throw a StackOverflowError exception.
● If the local method stack can be dynamically expanded and cannot apply for enough memory when trying to expand, or there is not enough memory to create the corresponding local method stack when creating a new thread, then the Java virtual machine will throw a OutOfMemoryError exception.

Native methods are implemented using C language.

Its specific method is to register the native method in the Native Method Stack and load the local method library when the Execution Engine is executed.
Insert image description here
When a thread calls a native method, it enters a new world that is no longer restricted by the virtual machine. It has the same permissions as the virtual machine.
Native methods can access the runtime data area inside the virtual machine through the native method interface.
It can even allocate any amount of memory directly from the heap of local memory using registers in the local processor.
Not all JVMs support native methods. Because the Java virtual machine specification does not clearly require the language, specific implementation method, data structure, etc. of the local method stack. If the JVM product does not intend to support native methods, there is no need to implement the native method stack.
In Hotspot JVM, the local method stack and virtual machine stack are directly combined into one.

Guess you like

Origin blog.csdn.net/picktheshy/article/details/132653956