Bonjour à tous
Je suis en train de me familiariser avec JNI en réalisant de petits exemples, et j'ai rencontré un comportement que je n'arrive pas à expliquer.
Mon exemple est très très simple à comprendre
Java demande à ma fonction native en C d’écrire "COUCOU".
A la fin de la sortie de la fonction native, Java écrit "Hello"
voici le code natif en C
le .h
le .c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 /* DO NOT EDIT THIS FILE - it is machine generated */ #include <jni.h> /* Header for class TestJavaCPP */ #ifndef _Included_TestJavaCPP #define _Included_TestJavaCPP #ifdef __cplusplus extern "C" { #endif /* * Class: TestJavaCPP * Method: SayHello * Signature: ()V */ JNIEXPORT void JNICALL Java_TestJavaCPP_SayHello (JNIEnv *, jobject); #ifdef __cplusplus } #endif #endif
et le java
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 #include <jni.h> #include <stdio.h> #include <TestJavaCPP.h> JNIEXPORT void JNICALL Java_TestJavaCPP_SayHello(JNIEnv *env, jobject obj) { printf("COUCOU\n"); }
le résultat en console est déconcertant puisque j’obtiens :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class TestJavaCPP { public static void main(String[] args) { TestJavaCPP HelloRun = new TestJavaCPP(); int i = 10; while(i>0) { HelloRun.SayHello(); System.out.println("HELLO"); --i; } } public TestJavaCPP() { System.out.println("new JAVA TestJavaCPP"); } public native void SayHello(); static { System.loadLibrary("TestJavaCPP"); } }
new JAVA TestJavaCPP
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
HELLO
COUCOU
COUCOU
COUCOU
COUCOU
COUCOU
COUCOU
COUCOU
COUCOU
COUCOU
COUCOU
au lieu de:
new JAVA TestJavaCPP
COUCOU
HELLO
COUCOU
HELLO
COUCOU
HELLO
COUCOU
HELLO
...
D'ou provient ce probleme? Est-ce dû au fait que mon processeur est multi-core? Comment contourner ce problème?
Merci d'avance pour vos réponses !!!
Partager