Is it safe to cache a JNIEnv object across multiple JNI calls? For example, can I d开发者_运维技巧o something like
void foo(JNIEnv* env)
{
static JNIEnv* cached;
if( ! cached )
cached = env;
/* use cached */
}
Of course, the code I have in reality isn't anything this silly, but the above example does demonstrate the usage case well.
Yes, you can, but only within a single thread. Your cache can't be as simple as the above unless you are sure, somehow, that your native code will only be called on a single thread. Otherwise you need to use thread-local storage to hold your JNIEnv references.
精彩评论