akonsu
akonsu

Reputation: 29536

Does JVM halt when calling native code?

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?

Upvotes: 6

Views: 706

Answers (1)

apangin
apangin

Reputation: 98324

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.

Upvotes: 13

Related Questions