Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JNI and Threads  RSS feed

 
Mandan Happy
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Dear Geeks,
I have a Scenaria wherein I need to invoke a native function from a Java Program,which created a pthread and the thread calls back a java function. It worked fine on Solaris but on Linux its giving me Segmentation violation.
. A.java has a native function foo()
. foo creates a thread named callBack() passing it the env pointer
. callBack() invoked A.hello();
Are there any problems if I pass the env pointer to a native thread (pthread) . [Pl gimme the reasons too]
Any help in this regard,narrow or wide ,Tall or Short ,all are welcome.
Thanks
Mandan Happy
 
John Harby
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JNIEnv pointer will only be valid in the current thread. Maybe you aren't violating this since it worked on Solaris but did you try passing the JNIEnv value?
 
Jay Chou
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same question,how to get the right JNIEnv in another thread in c++?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The use of Global References in JNI is discussed in Sun's JNI Tutorial. Global references are the only kind that you can pass between threads or that can persist across native calls.
 
Jay Chou
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
The use of Global References in JNI is discussed in Sun's JNI Tutorial. Global references are the only kind that you can pass between threads or that can persist across native calls.


Thank you for your reply,i can get jobject's global reference using NewGlobalRef(jobject lobj), but how can i get global reference of JNIEnv *env,I'll use env in another thread.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not too complicated...

The JNIEnv method GetJavaVM() gives you a JavaVM pointer, which is global and valid in any thread. You can pass the JavaVM pointer to another thread, and the thread can use it to call AttachCurrentThread(). AttachCurrentThread gives you a valid JNIEnv for that thread!
 
Jay Chou
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
It's not too complicated...

The JNIEnv method GetJavaVM() gives you a JavaVM pointer, which is global and valid in any thread. You can pass the JavaVM pointer to another thread, and the thread can use it to call AttachCurrentThread(). AttachCurrentThread gives you a valid JNIEnv for that thread!


Thank you ,I think i'll resolve it soon.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!