Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-28-2008, 08:02 AM
Member
 
Join Date: Jan 2008
Posts: 1
skaterxu is on a distinguished road
If JNI thread call the java object in another thread, it will crash.

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 08:07 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
The Java Jokes Thread DonCash Jokes and Funny Things 4 06-30-2008 03:49 PM
data from the main/GUI thread to another runnin thread... cornercuttin Threads and Synchronization 2 04-23-2008 11:30 PM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 10:31 AM
main() thread in Java JavaForums Java Blogs 0 12-17-2007 01:21 PM
how to access to an object from a thread tamayo New To Java 1 07-24-2007 05:24 AM


All times are GMT +3. The time now is 02:51 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org