(JNI with) multiple threads are accessing a method of a DLL loaded.....

Hi all,

I am new to this forum..

I have a problem...iam briefing it as follows.

I had a C application, developed on WindowsNT platform.

I have to make that JNI Capable ...

Task Done:

I had written java wraper classes..and i declared a native method .

I developed DLL from the available C code , by replacing main() method

with the signature i got from javah -jni classname (nativemethod).

I am successfully loading the DLL in the main class.

I am able to get my work with a single java thread that calling the native method.

When i try to make it multithread capable , i,e 2 or more threads from java side calling

same native method , i'm not..able to achieve this.

Can you please suggest me what ever iam doing is correct or not?

And any pointers on this which will help my work...

Thanks in advance

Venkat

[927 byte] By [ramisetty] at [2007-9-26 2:28:28]
# 1

Are you passing the JNIEnv pointer around..? You can't pass local references of the JNIEnv pointer from one thread to another.

There are two possible ways to handle JNI calls in a multithreaded environment. You can have an synchronized block in the Java code or use the equivalent functions in the C code

Try using the MonitorEnter() to enter the monitor and MonitorExit() when exiting the monitor in the C code. Each method takes a jobject as an argument and blocks if another thread has already entered the monitor associated with the jobject.

if (*env) ->MonitorEnter(env,jObj) != JNI_OK)

{

... rest of code including error handling

}

if (*env) -> MonitorExit(env,jObj) != JNI_OK)

{

... rest of code including error handling

}

hope this helps..

..geetha

geethajothi at 2007-6-29 9:44:14 > top of Java-index,Core,Core APIs...
# 2
Hi geetha,Iam not passing env pointer around...I tried with CreateMutex() / ReleaseMutex() synchronization methods ...I am new to this ...I will try what ever you suggested alsoThanks and cheersVenkat
ramisetty at 2007-6-29 9:44:14 > top of Java-index,Core,Core APIs...
# 3
What do you mean you can't achieve it?Does it compile? Does it run but you get an exception? Does it run but the result isn't what you expect?Is the dll thread enabled- is it thread safe?
jschell at 2007-6-29 9:44:14 > top of Java-index,Core,Core APIs...
# 4

Hi,

see, it compiles and running fine when i called that DLL method by one thread.

When i use more than 2 threads calling same dll method, iam not able to get the desired

result. (the dll is detaching by process before finishing all threads execution).

Note:

I found that the main thread is exiting before all the child threads finished their execution.

I will look into this dll method ...and Thanks for your support.

Venkat

ramisetty at 2007-6-29 9:44:14 > top of Java-index,Core,Core APIs...
# 5
As the comment above said, is the DLL multi threaded safe ?
scottcoleman1973 at 2007-6-29 9:44:14 > top of Java-index,Core,Core APIs...
# 6

Hi,

I know how to use the synchronization jni constructs. But I just found out, after reading a C Win32 book ;-), that this will only work for multi-threads apps where the dll is NOT re-entrant. According to the book I have to use thread-local-storage (tls). But it so complicated!!! Any other ideas?

TIA,

Reggie

reggiehg at 2007-6-29 9:44:14 > top of Java-index,Core,Core APIs...