JNI calls interleaving with regular Java calls - what is the execution order?

I recently experimented with JNI to port some existing C ++ libraries.

As part of my testing, I created a simple helloworld program. I call a simple built-in function in C ++ that simply prints messages. I'm a little curious about the behavior I observed during the execution of the program - it seems that all messages and answers to functional functions are printed after Java System.out.print . Is it because native calls are made after Java calls, or am I just ignoring this behavior?

 public static void main(String[] args) { HelloWorld app = new HelloWorld(); System.out.println("say"); app.print(); System.out.println("what"); app.print(); } 

The result is as follows:

 say what hola, world ! hola, world ! 

The native function is as follows:

 Java_HelloWorld_print(JNIEnv *env, jobject obj) { printf("hola, world !\n"); return; } 
+6
source share
1 answer

This is because native calls are made after Java calls

No, this is almost certainly due to how the output is buffered on the C ++ and Java sides.

The order in which the calls are made is exactly the same as in your code (Java, C ++, Java, C ++).

+3
source

Source: https://habr.com/ru/post/914399/


All Articles