多线程中的JNIEnv

在一个线程中,通过C++回调Java时,通过一个全局的env来操作java对象,这样程序会挂掉。
文档中出现了这个很好的说明了这一点
A JNIEnv pointer is only valid in the thread associated with it. You must not
pass this pointer from one thread to another, or cache and use it in multiple
threads.

解决:

先通过另外一个含有JNIEnv 的线程中获取一个JavaVM *jvm;然后设外全局变量

然后在另外的一个线程中
JNIEnv *env;
jvm->AttachCurrentThread(&env, NULL);
在通过这个env操作Java对象

你可能感兴趣的:(多线程)