Is Java VM interface pointer returned by GetJavaVM() thread safe?

665 views
Skip to first unread message

Lee Leclair

unread,
Jul 14, 2010, 10:32:11 AM7/14/10
to android-ndk
I am currently caching the Java VM pointer returned by GetJavaVM() as
a static global reference. Is it safe to access this reference from
multiple threads, or do I need to explicitly protect it with a mutex?

fadden

unread,
Jul 14, 2010, 1:35:30 PM7/14/10
to android-ndk
The JavaVM* is an opaque pointer to a C struct, not an object
reference, but maybe I'm just being confused by your word choice.

The only thing you're allowed to do with a JavaVM* is pass it into JNI
functions, which will take care of any necessary synchronization.

Lee Leclair

unread,
Jul 14, 2010, 3:35:06 PM7/14/10
to android-ndk
Sorry, just to clarify: so if I was to do something like:
pJVM->AttachCurrentThread(...)

from multiple threads, the underlying implementation of
AttachCurrentThread() should take care synchronization for me?

Onur Cinar

unread,
Jul 14, 2010, 4:26:22 PM7/14/10
to andro...@googlegroups.com

Hi Lee,

Well, I don't really know much about the internals of Dalvik, but the method that you are planing to call (AttachCurrentThread) is most likely designed to be thread safe by nature since it will always be called by non JVM threads.

In my application, I'm not using any specific locking mechanism around that, it is working fine for me.  If really would like to find out for sure, I believe that the best place would be the Dalvik source code.

git://android.git.kernel.org/platform/dalvik.git

I hope it helps.

Best regards,

-onur


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.


---
www.zdo.com

Justin Buser

unread,
Dec 7, 2012, 5:09:21 PM12/7/12
to andro...@googlegroups.com
I think you may be looking at this in reverse, you're attaching your thread to the Dalvik, not vice/versa. In other words it will allow you to operate as you would normally inside of a new thread.

David Turner

unread,
Dec 7, 2012, 5:19:04 PM12/7/12
to andro...@googlegroups.com
As fadden said, it's not a reference, but you should be able to use it with AttachCurrentThread() without wrapping it around a mutex. Note that all JNIEnv* pointers are thread-specific, so don't cache these into static globals.
Reply all
Reply to author
Forward
0 new messages