Re: JNI and Java Threads

From: nos (nos_at_nospam.com)
Date: 01/20/04

  • Next message: Jon A. Cruz: "Re: JNI and Java Threads"
    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?


  • Next message: Jon A. Cruz: "Re: JNI and Java Threads"

    Relevant Pages

    • 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 methods ...
      (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)