Re: JNI and Java Threads
From: nos (nos_at_nospam.com)
Date: 01/20/04
- Previous message: chris: "Re: Weird problem - commenting out doesn't work..."
- In reply to: Rob: "JNI and Java Threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 19 Jan 2004 23:51:35 GMT
"Rob" <.@.com> wrote in message
news:400c3928$0$2439$cc9e4d1f@news.dial.pipex.com...
> 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.
>
>
Does the 'c' program have to be re-entrant?
- Previous message: chris: "Re: Weird problem - commenting out doesn't work..."
- In reply to: Rob: "JNI and Java Threads"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|