简体   繁体   English

JNI调用与常规Java调用交错 - 执行顺序是什么?

[英]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. 我最近一直在试验JNI,以便移植一些现有的C ++库。

As part of my testing I created a simple 'helloworld' program. 作为测试的一部分,我创建了一个简单的'helloworld'程序。 I am calling a simple native function in C++, that just prints messages. 我在C ++中调用一个简单的本机函数,它只打印消息。 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. 我对执行程序时观察到的一些行为感到好奇 - 似乎所有本机函数消息/响应都在Java System.out.print之后打印出来。 Is this because native calls are executed after Java calls, or shall I just ignore this behavior? 这是因为本机调用是在Java调用之后执行的,还是我应该忽略这种行为?

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;
}

Is this because native calls are executed after Java calls 这是因为本机调用是在Java调用之后执行的

No, it almost certainly has to do with how the output gets buffered on the C++ and Java sides. 不,它几乎肯定与输出如何在C ++和Java端缓冲有关。

The execution order of the calls is exactly as it appears in your code (Java, C++, Java, C++). 调用的执行顺序与您的代码(Java,C ++,Java,C ++)中的执行顺序完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM