Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have been experimenting with JNI recently, in order to port some existing C++ libraries.

As part of my testing I created a simple 'helloworld' program. I am calling a simple native function in C++, that just prints messages. I am a bit curious about some behavior I have observed while executing the program - it seems that all the native function messages/responses gets printed after Java System.out.print's. Is this because native calls are executed after Java calls, or shall I just ignore 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 output looks like this:

say
what
hola, world !
hola, world !

The native function is as follows:

Java_HelloWorld_print(JNIEnv *env, jobject obj) {
    printf("hola, world !\n");
    return;
}
like image 983
padam thapa Avatar asked Apr 28 '12 05:04

padam thapa


1 Answers

Is this because native calls are executed after Java calls

No, it almost certainly has to do with how the output gets buffered on the C++ and Java sides.

The execution order of the calls is exactly as it appears in your code (Java, C++, Java, C++).

like image 104
NPE Avatar answered Oct 31 '22 03:10

NPE