牛骨文教育服务平台(让学习变的简单)
博文笔记

JNI异步条件下(多线程/回调函数),如何取得JNI Env

创建时间:2013-01-04 投稿人: 浏览次数:2580

JNI使用(异步条件下)

JNIJVM多线程 1、JNI异步条件下(多线程/回调函数),如何取得JNI Env 

使用AttachCurrentThread()函数。 

示例代码: 

        #ifdef JNI_VERSION_1_4 
            jint res = cached_jvm->AttachCurrentThread((void**)&env, NULL); 
        #else 
            jint res = cached_jvm->AttachCurrentThread(&env, NULL); 
        #endif; 
        if (env == NULL) 
         return; 

        jclass clsSctp = env->FindClass("com/sunrising/nettest/netpackage/implement/SCTPTransImplement"); 
        if (clsSctp == NULL) 
        { 
         ThrowNullPointerException(env, "SCTP class is null"); 
            cached_jvm->DetachCurrentThread(); 
         return; 
        } 
        jmethodID mid = env->GetStaticMethodID(clsSctp, "communicationErrorNotify", "(JJ)V"); 
        if (mid == 0) { 
            ThrowNullPointerException(env, "Method communicationErrorNotify() is null"); 
            cached_jvm->DetachCurrentThread(); 
            return; 
        } 
        env->CallStaticVoidMethod(clsSctp, mid, jlAssociationID, jlErrorType); 
        cached_jvm->DetachCurrentThread(); 

2、使用GetStringUTFChars()之后要注意释放内存 

使用上述函数之后,要使用ReleaseStringUTFChars()函数释放字符串内存。 

示例代码: 

   pBuffer = env->GetStringUTFChars(jsAmrFile, &isCopy); 
   strncpy_s(szAmrFile, MAX_STRING, pBuffer, MAX_STRING - 1); 
   if (isCopy & 0xFF) 
   { 
    env->ReleaseStringUTFChars(jsAmrFile, pBuffer); 
   }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。