JNI and Java Threads

From: Rob (._at_.com)
Date: 01/19/04


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.



Relevant Pages

  • Re: JNI and Java Threads
    ... > I have written a native implementation in C, and a java wrapper class. ... > single thread, however, my application requires calling the wrapper ...
    (comp.lang.java)
  • Re: JNI - Passing and returning complex values
    ... |> |> This is one of the uglier parts of Java. ... You have to write a very thin C wrapper which calls the C ... The trick is to write a reference to the wrapped ... ~ private long getCPtrAndAddLeftDaughterReference(mgTreeNode daughter) { ...
    (comp.lang.java.programmer)
  • Re: Java <-> MFC?
    ... The installation process for my MFC app couldn't add the ... calling DestroyJavaVM() isn't helpful - it doesn't return - hints in the ... Java application to terminate, which they aren't going to; ...
    (comp.lang.java.programmer)
  • Re: JNI - Passing and returning complex values
    ... |> |> This is one of the uglier parts of Java. ... |> | This is what SWIG does. ... You have to write a very thin C wrapper which calls the C ... On the C side, all references are fine. ...
    (comp.lang.java.programmer)
  • Re: Interest in .Net connectivity?
    ... value for efficiency sake, and as you describe the image stripping issue. ... The system will then ensure that the dynamically-generated wrapper ... for a given Java class is a subclass of the most specific available static ... way to cure that would be to add Mixins to Smalltalk. ...
    (comp.lang.smalltalk.dolphin)