Java JNI system.out和printf行为

Java JNI system.out和printf行为,java,c,java-native-interface,Java,C,Java Native Interface,我正在编写一个程序,它使用JNI与一个简单的c程序接口。我创建了以下程序: public static void main(String[] args) { Hello h = new Hello(); System.out.println("before"); int number = h.sayHello(); System.out.println(number); System.out.println("after"); } 及 令我惊讶的是,这个

我正在编写一个程序,它使用JNI与一个简单的c程序接口。我创建了以下程序:

public static void main(String[] args) {
    Hello h = new Hello();
    System.out.println("before");
    int number = h.sayHello();
    System.out.println(number);
    System.out.println("after");
}

令我惊讶的是,这个程序返回:

before
10
after
Hello JNI

对我来说,这是非常奇怪的,因为很明显,c程序是在“before”和“after”语句之间执行的(打印的是数字10)。但是为什么调用printf语句时不执行它呢。它是否因为只允许一个程序同时写入输出而被jvm阻塞了?有没有办法纠正这种行为?

有。您需要调用
flush

C
中,即调用-

Java
中,这只是在流中-

System.out.println("before");
System.out.flush();
printf("Hello JNI\n");
fflush(stdout);
return 10;
System.out.println("before");
System.out.flush();