JVM from entry to proficient (7) native method interface

One, local method interface

In talking about the Java virtual machine is running before the data area native method stacks, Let's say outside the data area is running a thing called the native method interface abbreviation of JNI (Java Native Interface).

JVM architecture overview

What is the local method?

  1. Simply put, a Native Method is an interface for Java to call non-Java code

  2. A Native Method is a Java method: the method is implemented in 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." (The native method is a non-Java method, and its specific implementation is the implementation of non-java code.)

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

  6. The role of the native interface is to integrate different programming languages ​​for Java, and its original intention is to integrate C/C++ programs.

Examples of native methods

The getClass() method of the Object class

public final native Class<?> getClass();

The start() method of the Thread class

    public synchronized void start() {
    
    
        /**
         * This method is not invoked for the main method thread or "system"
         * group threads created/set up by the VM. Any new functionality added
         * to this method in the future may have to also be added to the VM.
         *
         * A zero status value corresponds to state "NEW".
         */
        if (threadStatus != 0)
            throw new IllegalThreadStateException();

        /* Notify the group that this thread is about to be started
         * so that it can be added to the group's list of threads
         * and the group's unstarted count can be decremented. */
        group.add(this);

        boolean started = false;
        try {
    
    
            start0();
            started = true;
        } finally {
    
    
            try {
    
    
                if (!started) {
    
    
                    group.threadStartFailed(this);
                }
            } catch (Throwable ignore) {
    
    
                /* do nothing. If start0 threw a Throwable then
                  it will be passed up the call stack */
            }
        }
    }

    private native void start0();
  • Code examples illustrate how to write Native methods
  • It should be noted that the identifier native can be used in conjunction with other java identifiers, except for abstract
/**
 * 本地方法
 */
public  class IHaveNatives {
    
    

    //abstract 没有方法体
    public abstract void abstractMethod(int x);

    //native 和 abstract不能共存,native是有方法体的,由C语言来实现
    public native void Native1(int x);

    native static public long Native2();

    native synchronized private float Native3(Object o);

    native void Native4(int[] array) throws Exception;

}

2. Why use Native Method?

Java is very convenient to use, but some levels of tasks are not easy to achieve with Java, or when we are very concerned about the efficiency of the program, then the problem comes.

2.1 Interaction with the Java environment

  1. Sometimes Java applications need to interact with the hardware environment outside Java, which is the main reason for the existence of native methods. You may need to think about Java and some of the underlying system case when, as certain hardware or operating system to exchange information.
  2. The native method is just such a communication mechanism: it provides us with a very concise interface, and we do not need to understand the cumbersome details of Java applications.

2.2 Interaction with the operating system

  1. JVM supports the Java language itself and runtime libraries. It is the platform on which Java programs live. It consists of an interpreter (interpreting bytecode) and some libraries connected to native code.
  2. 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.
  3. By using the native method, we were able to use Java to realize the interaction between jre and the underlying system, and even some parts of the JVM were written in C.
  4. Also, if we want to use some of the features of the operating system that the Java language itself does not provide encapsulation, we also need to use native methods.

2.3 Sun’s Java

  1. Sun's interpreter is implemented in C, which allows 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.lang.Threadof setPriority()methods is implemented in Java, but it is the type of implementation calls in the local method setPriority0 (). This native method is implemented in C and is implanted inside the JVM on the Windows 95 platform. This native method will eventually call the Win32 setpriority() API.
  3. This is a specific implementation of a local method directly provided by the JVM, more often the local method is provided by an external dynamic link library (external dynamic link library) and then called by the JVM.

3. Current status

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 relatively rare in enterprise applications. Because the communication between heterogeneous domains is now very developed, for example, Socket communication can be used, or Web Service can also be used, etc., I will not introduce more here.

Guess you like

Origin blog.csdn.net/BeiisBei/article/details/108940696