In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.