JNI調用,線程相關

線程由C層pthread啓動的,須要DetachCurrentThread。java

JNIEnv *env;

 g_jvm->AttachCurrentThread(&env, NULL);  //必須AttachCurrentThread,env只能在本身的線程運行

 jclass clazz = getObjectClass(env, g_monitorObj);

 jmethodID onLoginedMethod = getMethodID(env, clazz,"onLogStateChged","(ILjava/lang/String;)V");

 jstring deviceId = (env)->NewStringUTF(TargDeviceID);

 env->CallVoidMethod(g_monitorObj, onLoginedMethod, CurState, deviceId);

 env->DeleteLocalRef(deviceId);

 g_jvm->DetachCurrentThread();

若由Java啓動的,不須要DetachCurrentThread。jvm

JNIEnv *env;

 g_jvm->AttachCurrentThread(&env, NULL);  //必須AttachCurrentThread,env只能在本身的線程運行

 jclass clazz = getObjectClass(env, g_monitorObj);

 jmethodID onLoginedMethod = getMethodID(env, clazz,"onLogStateChged","(ILjava/lang/String;)V");

 jstring deviceId = (env)->NewStringUTF(TargDeviceID);

 env->CallVoidMethod(g_monitorObj, onLoginedMethod, CurState, deviceId);

 env->DeleteLocalRef(deviceId);

 //g_jvm->DetachCurrentThread();
相關文章
相關標籤/搜索