java - Properly using JNI DetachCurrentThread in performance paths -


i writing multi threaded c++ program uses jni communicate java code. according design following method (run()) run thread , after 1 run native thread might switch. (round robin style thread allocation)

bool javamodule::run() {     initobjects();     /*attaching current thread     *and checking jvm exceptions     *for each run     */     interpreter::getenv()->callobjectmethod(objid, msgid, null);     if (interpreter::getenv()->exceptioncheck())     {         getlogger().error("err: jvm exception occurred when running script");         return false;     }      //detaching current thread     //there performance hit when detaching environment each time     interpreter::detachenv();     return true; } 

this call in performance path of program , if try attach , detach environment current thread there big performance issue. attachment happens in getenv() looks this.

static jnienv* interpreter::getenv() {     jnienv *env;     int status = jvm->getenv((void**)&env, jni_version_1_6);     if (status < 0)     {         status = jvm->attachcurrentthread((void**)&env, null);         if (status < 0)         {             return nullptr;         }     }     return env; } 

jvm class member defined static javavm* jvm;

the detachment code looks below.

static bool interpreter::detachenv() {     if (jvm->detachcurrentthread() == jni_ok)     {         return true;     }     return false; } 

at level of code has no idea threads , @ thread creation level doesn't have idea jvm.

my question solution detach threads safely without performance hit?

don't attach or detach performance critical threads jvm. jvm needs synchronise garbage collector , garbage collector 1 massive single threaded critical section.

if need performance critical thread communicate jvm need kind of asynchronous messaging.

you attach , detach thread @ thread creation & joining still have synchronise gc in other methods.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -