(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]

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
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