JNI and Java Threads
From: Rob (._at_.com)
Date: 01/19/04
- Next message: Jared Dykstra: "Re: DBCP Configuration with Tomcat 4.1.27 and mySQL"
- Previous message: PerfectDayToChaseTornados: "Re: Code shared by 2 EJB, where to put ?"
- Next in thread: Jon A. Cruz: "Re: JNI and Java Threads"
- Reply: Jon A. Cruz: "Re: JNI and Java Threads"
- Reply: George Fox: "Re: JNI and Java Threads"
- Reply: nos: "Re: JNI and Java Threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 19 Jan 2004 20:08:09 -0000
Hi there,
Firstly, apologies for x-post but I'm hoping the post is relevant to both
groups...
I have written a native implementation in C, and a java wrapper class. This
all works fine and correctly while I am calling the wrapper methods from a
single thread, however, my application requires calling the wrapper methods
for the native code from several different (java) threads.
And this is where I get stuck! - I am getting garbage values back from
native unless I use the same thread each time.
I have implemented something whereby I start a new thread, and then
wait/notify as each of the other requesting threads contact this single
thread, and then that single threads toodles off to native, but that causes
problems with waiting for return values and also makes the code far more
complicated that I believe it should be.
I have seen mentions of the functions 'JNI_GetCreatedJavaVMs' and
'AttachCurrentThread()', but as far as I can see, these are mainly for
contacting the JVM from the C and not the other way around. (I may well be
(and hope I am!) wrong).
This is a passage from a book I have which seems to answer my question,
kinda:
"Since a native method may be called from different threads in a Java
application, each different invocation of that native method will have a
different JNIEnv pointer passed to it. Passing this information to another
thread will result in unpredictable behaviour.
On the other hand, for a given method, from a given thread, the JVM
guarantees that the same JNIEnv pointer is passed on each call to that
method."
This seems to say that what I want is not possible, but regardless its what
I need lol so one way or another I need to figure it out. (I'm calling
native methods from the GUI and from event register calls etc.) Can anyone
offer any relatively clean work arounds or suggestions to how this can be
done? (other than the messy way I've tested as suggested above?)
Thanks for any anyone can offer, I've been on this for days!!! I'm hoping
I've been way off base and that the answer is simple.... but hey, I also
understand that this is computing...
Rob.
- Next message: Jared Dykstra: "Re: DBCP Configuration with Tomcat 4.1.27 and mySQL"
- Previous message: PerfectDayToChaseTornados: "Re: Code shared by 2 EJB, where to put ?"
- Next in thread: Jon A. Cruz: "Re: JNI and Java Threads"
- Reply: Jon A. Cruz: "Re: JNI and Java Threads"
- Reply: George Fox: "Re: JNI and Java Threads"
- Reply: nos: "Re: JNI and Java Threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|