Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to call Java method in multi-threaded C++ application?

843829Nov 7 2001 — edited Nov 7 2001
I have a Multi-threaded C++ application that needs to call Java methods in different threads. I first load JVM in one thread, then my code looks like this:

jclass cls = m_env->FindClass(chartEngineClass);
jmethodID mid = m_env->GetMethodID(cls, NOI18N("<init>"), NOI18N("()V"));
jobject instance = m_env->NewObject(cls, mid);
m_renderID = m_env->GetMethodID(cls, renderMethod, NOI18N("([B[BIIIZIII)[B"));

Afterward, I can call the method. My question is, if I want to use the JVM in another thread, can I use the same m_env I got back from the original thread? I did make the instance object global so that other threads can access it. It seems that I can use the old env for a while in other threads, and then my app crashes, in a call m_env->NewByteArray().

Assume I can not use the m_env in another thread, does it mean I have to call FindClass, GetMethodID, NewObject and GetMethodID again in another thread? That would be a lot of overhead for me.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 5 2001
Added on Nov 7 2001
1 comment
226 views