Results 1 to 1 of 1
  1. #1
    skaterxu is offline Member
    Join Date
    Jan 2008
    Posts
    1
    Rep Power
    0

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

    :confused:
    Hello guys:

    I have encountered 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_ROUTIN E)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;
    }
    }



    Remove the jclass in the stack space also have this problem.

    while(1){
    jclass cls= env->FindClass("Sample1");
    printf("Here!");

    }

    Crash at the malloc.c
    if (__active_heap == __SYSTEM_HEAP) {
    return HeapAlloc(_crtheap, 0, size ? size : 1);
    }

    java version "1.5.0_07"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
    Java HotSpot(TM) Client VM (build 1.5.0_07-b03, mixed mode)
    Last edited by skaterxu; 01-28-2008 at 07:07 AM.

Similar Threads

  1. The Java Jokes Thread
    By DonCash in forum Entertainment
    Replies: 12
    Last Post: 09-03-2012, 05:56 PM
  2. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 10:30 PM
  3. how to access to an object from a thread
    By tamayo in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 04:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •