我在写一个使用JNI与Java代码进行通信的多线程C++程序。根据设计,以下方法(run())由线程运行,一次运行后,本地线程可能会切换。 (轮循式的线程分配)在性能路径中正确使用JNI DetachCurrentThread
bool JavaModule::run()
{
initObjects();
/*Attaching to the current thread
*and checking for JVM exceptions
*for each run
*/
Interpreter::getEnv()->CallObjectMethod(objid, msgid, NULL);
if (Interpreter::getEnv()->ExceptionCheck())
{
getLogger().error("ERR: JVM Exception occurred when running the script");
return false;
}
//Detaching from the current thread
//There is a performance hit when detaching the Environment each time
Interpreter::detachEnv();
return true;
}
此调用程序的性能路径,如果我尝试连接和分离从当前线程环境存在较大的性能问题。附件在getEnv()中看起来像这样。
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是定义为static JavaVM* jvm;
类成员的分离的代码看起来如下。
static bool Interpreter::detachEnv()
{
if (jvm->DetachCurrentThread() == JNI_OK)
{
return true;
}
return false;
}
在这个级别的代码有没有关于线程和线程的创建水平不会有关于JVM的思想观念。
我的问题是什么将是一个很好的解决方案安全地分离线程没有性能影响?
您不应该分离未明确附加的线程。也就是说,你需要跟踪你的'JNIEnv *'是来自'GetEnv'还是'AttachCurrentThread',并且在后一种情况下只调用'DetachCurrentThread'。 – Michael
@Michael喜欢在流程结束时保持缓存并清理所有内容? –
我已经发布了有关过去的答案:http://stackoverflow.com/a/30026231/1524450 – Michael