Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does JVM halt when calling native code?

Tags:

java

jvm

Are there any performance implications when calling machine code from Java's bytecode? Since machine code is "unmanaged", and it is not "aware" of Java's internals, maybe this causes JVM's internal schedulers to pause or something like that?

I am new to Java, so maybe this is not even relevant, please help.

I know that in Erlang they have this problem, where the VM basically stops while it is making calls to machine code. I hope this is not the case with Java. Is it?

like image 740
akonsu Avatar asked Aug 29 '16 14:08

akonsu


People also ask

What is native code in JVM?

The Java Native Interface (JNI) establishes a well-defined and platform-independent interface between the two. Native code can be used together with Java in two distinct ways: as "native methods" in a running JVM and as the code that creates a JVM using the "Invocation API".

Is Java a native code?

This chapter introduces the Java Native Interface (JNI). The JNI is a native programming interface. It allows Java code that runs inside a Java Virtual Machine (VM) to interoperate with applications and libraries written in other programming languages, such as C, C++, and assembly.


1 Answers

Threads running native code will not halt JVM.

As soon as JNI method is called, Java thread switches to _thread_in_native state. Threads in this state are not counted during safepoint operations, i.e. when a stop-the-world event occurs (like garbage collection), JVM does not stop these threads. Conversely, threads in native state cannot stop JVM unless they perform a JNI upcall. When a native method returns, the thread switches back to _thread_in_Java state.

like image 198
apangin Avatar answered Oct 07 '22 06:10

apangin