Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

If JNI thread call the java object in another thread, it will crash.

843829Jan 27 2008 — edited Jan 28 2008
Hello guys:

I have countered a problem in JNI. I have used the java to call JNI and let the JNI call back in the same thread, there is nothing wrong.
But now I want to let the JNI call a java object in another thread.
On my desktop, if I call the java method in my JNI, the windows will tell me there is sth wrong with the JVM. If I only call get the class, the client will run 32 times, I am not sure whether there is sth wrong with my code or there is a bug in JNI?

My JNI
JNIEXPORT jint JNICALL Java_Sample1_intArrayMethod
(JNIEnv *env, jobject obj, jintArray array) {
int i, sum = 0;
HANDLE hThread;
unsigned long * p=NULL;

hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFunc,env,0,p);
WaitForSingleObject(hThread,INFINITE);

jsize len = env->GetArrayLength(array);
jint *body = env->GetIntArrayElements(array, 0);
for (i=0; i<len; i++)
{
sum += body;
}

env->ReleaseIntArrayElements(array, body, 0);


return sum;
}

long WINAPI threadFunc(JNIEnv *env){
jclass cls;
while(1){
cls = env->FindClass("Sample1");
printf("Here!");

}
return 0;
}

My Sample1.java


public class Sample1
{ public native int intMethod(int n);
public native boolean booleanMethod(boolean bool);
public native String stringMethod(String text);
public native int intArrayMethod(int[] intArray);

public static void main(String[] args) {
System.loadLibrary("Sample1");
Sample1 sample = new Sample1();
int sum = sample.intArrayMethod(new int[]{1,1,2,3,5,8,13} );
System.out.println("intArrayMethod: " + sum);
}

public static int intMethod2(int n){
return n*n;
}
public static boolean booleanMethod2(boolean bool){
return !bool;
}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 25 2008
Added on Jan 27 2008
8 comments
1,493 views