Get the App
SLTechnology News&Howtos  ›  Development  › 

How to cache fields and ID when Android JNI is called

Shulou Source: shulou.com Published: 2022-05-31 21:07:01 09月20日 Update

This article mainly explains how to cache fields and ID when Android JNI is called. The explanation in this article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn how to cache fields and ID when Android JNI is called.

Cache when in use

To cache when you use it is to look it up once during the call, and then cache it as a static variable so that it will have been initialized the next time it is called.

The cache does not expire until memory is freed.

Extern "C" JNIEXPORT void JNICALLJava_com_glumes_cppso_jnioperations_CacheFieldAndMethodOps_staticCacheField (JNIEnv * env, jobject instance, jobject animal) {static jfieldID fid = NULL; / / declared as static variable for caching / / both / / jclass cls = env- > GetObjectClass (animal); jclass cls = env- > FindClass ("com/glumes/cppso/model/Animal"); jstring jstr; const char * c_str / find if (fid = = NULL) {fid = env- > GetFieldID (cls, "name", "Ljava/lang/String;"); if (fid = = NULL) {return;}} else {LOGD ("field id is cached");} jstr = (jstring) env- > GetObjectField (animal, fid); c_str = env- > GetStringUTFChars (jstr, NULL); if (c_str = = NULL) {return;} env- > ReleaseStringUTFChars (jstr, c_str) Jstr = env- > NewStringUTF ("new name"); if (jstr = = NULL) {return;} env- > SetObjectField (animal, fid, jstr);}

Cached by declaring it as a static variable. However, this caching method obviously has its drawbacks. When multiple callers call at the same time, there will be multiple caches, and each call should be checked to see if it has been cached.

Caching on initialization

Caching on initialization, that is, caching when the class loads. When a class is loaded into memory, the static code block of the class is called first, so it can be cached in the static code block of the class.

For example:

Public class CacheFieldAndMethodOps extends BaseOperation {static {initCacheMethodId (); / / caching in static code blocks} private static native void initCacheMethodId ();}

In a static code block, you can cache the desired field id or method id as a global variable.

The specific code is as follows:

/ / global variable, as cache method idjmethodID InstanceMethodCache;// initializes the cache method idextern "C" JNIEXPORT void JNICALLJava_com_glumes_cppso_jnioperations_CacheFieldAndMethodOps_initCacheMethodId (JNIEnv * env, jclass type) {jclass cls = env- > FindClass ("com/glumes/cppso/model/Animal"); InstanceMethodCache = env- > GetMethodID (cls, "getName", "() Ljava/lang/String;");}

The method id is cached as a global variable directly in JNI, so that when it is called again, there is no need to make a lookup again, and it avoids the situation that multiple threads will look up multiple times when calling at the same time.

Extern "C" JNIEXPORT void JNICALLJava_com_glumes_cppso_jnioperations_CacheFieldAndMethodOps_callCacheMethod (JNIEnv * env, jobject instance, jobject animal) {jstring name = (jstring) env- > CallObjectMethod (animal, InstanceMethodCache); const char * c_name = env- > GetStringUTFChars (name, NULL); LOGD ("call cache method and value is% s", c_name) } Thank you for reading. The above is the content of "how to cache fields and ID when Android JNI is called". After the study of this article, I believe you have a deeper understanding of how to cache fields and ID when Android JNI is called, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Cache variables fields code methods static that is situation learning memory content simultaneously multiple global drawbacks ideas articles ways more users Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Xiaomi Shulou Tech Info MariaDB