Note source: Silicon Valley JVM complete tutorial, millions of playback, the peak of the entire network (Song Hongkang details Java virtual machine)
Update: gitee.com/vectorx/NOT…
Codechina.csdn.net/qq_35925558…
Github.com/uxiahnan/NO…
[TOC]
5. Local method interface and local method stack
5.1. What are local methods?
Simply put, a Native Method is an interface to a Java call to 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. In C++, for example, you can tell the C++ compiler to call a C function with extern “c”.
A native method is a Java method whose implementation is provided by non-java code.
When you define a native method, you do not provide an implementation body (somewhat like defining a Java interface) because the implementation body is implemented outside of a non-Java language.
The purpose of the native interface is to merge different programming languages for Java use. It was originally intended to merge C/C++ programs.
For example,
public class IHaveNatives{
public native void methodNative1(int x);
public native static long methodNative2(a);
private native synchronized float methodNative3(Object o);
native void methodNative4(int[] ary) throws Exception;
}
Copy the code
Identifier Native can be used with other Java identifiers, except for Abstract
5.2. Why use Native Method?
Java is very convenient to use, but problems arise when there are levels of tasks that are not easy to implement in Java, or when we are concerned about program efficiency.
Interaction with the Java environment
Sometimes Java applications need to interact with environments outside of Java, which is the main reason native methods exist. You can think of a situation where Java needs to exchange information with some underlying system, such as an operating system or some hardware. The native method is just such a communication mechanism: it gives us a very concise interface, and we don’t have to learn the tedious details outside of Java applications.
Interaction with the operating system
The JVM supports the Java language itself and runtime libraries, which are the platform on which Java programs live. It consists of an interpreter (interpreting bytecode) and libraries that connect to native code. However, it is not a complete system, and it often depends on the support of an underlying system. These underlying systems are often powerful operating systems. By using native methods, we were able to implement the INTERACTION of the JRE with the underlying system in Java, and even some parts of the JVM were written in C. Also, we need to use native methods if we want to use some features of the operating system that the Java language itself does not provide for encapsulation.
Sun’s Java
Sun’s interpreter is implemented in C, which allows it to interact with the outside world just like normal C. The JRE is mostly implemented in Java, and it also interacts with the outside world through some native methods. For example, the setPriority() method of java.lang.Thread is implemented in Java, but its implementation calls the native setPriority() method of the class. This local method, implemented in C and built into the JVM, will eventually call the Win32 setPriority() ApI on The Windows 95 platform. This is a concrete implementation of a local method provided directly by the JVM, or more often by an external Dynamic Link Library and then called by JVw.
The status quo
This approach is being used less and less today, except in hardware-related applications, such as driving printers through Java programs or managing production equipment through Java systems, which are rare in enterprise applications. Because the communication between heterogeneous domains is very developed, for example, Socket communication can be used, Web Service can also be used, and so on, I will not introduce.
5.2. Local method stack
The Java virtual machine stack is used to manage calls to Java methods, while the local method stack is used to manage calls to local methods.
The local method stack is also thread-private.
Allows memory sizes to be implemented as fixed or dynamically scalable. (Same in terms of memory overflow)
- The Java virtual machine will throw a StackOverflowError if the thread request allocates more stack capacity than the maximum allowed by the local method stack.
- The Java virtual machine will throw an OutOfMemoryError if the local method stack can be extended dynamically and there is not enough memory to allocate when trying to extend it, or if there is not enough memory to create the corresponding local method stack when a new thread is created.
The native methods are implemented using the C language.
It is implemented by registering Native methods in the Stack and loading the local Method library at Execution Engine time.
When a thread calls a local method, it enters a whole new world that is no longer constrained by the virtual machine. It has the same rights as the VIRTUAL machine.
- Local methods can be accessed through the local method interfaceAccess the runtime data area inside the virtual machine.
- It can even use the registers in the local processor directly
- Allocate any amount of memory directly from the heap of local memory.
Not all JVMS support native methods. Because the Java virtual Machine specification does not specify the language, implementation, data structure, and so on of the native method stack. If the JVM product does not intend to support native methods, you may not need to implement a native method stack.
In Hotspot JVM, the local method stack and virtual machine stack are combined directly.