牛骨文教育服务平台(让学习变的简单)
博文笔记

JNI学习之-----Local and Global References

创建时间:2012-06-21 投稿人: 浏览次数:150

1.问: jni 支持多少种 references?

   答:

  • JNI supports three kinds of opaque references: local references, globalreferences, and weak global references.


2.问: Local 和 global references  的生命周期一样吗?

   答:

  • Local references areautomatically freed, whereas global and weak global references remain validuntil they are freed by the programmer.

3.问: local reference 的有效期是怎么样的? 它什么时候产生,什么时候消亡?

   答:A local reference is valid only within the dynamic context of the nativemethod that creates it, and only within that one invocation of the native method.All local references created during the execution of a native method will be freedonce the native method returns.

    

Local references are also only valid in the thread that creates them. A localreference that is created in one thread cannot be used in another thread. It is a pro-gramming error for a native method to store a local reference in a global variableand expect another thread to use the local reference.

4.问: 请你介绍一下global references的基本情况?

   答: 

You can use a global reference across multiple invocations of a native method. Aglobal reference can be used across multiple threads and remains valid until it isfreed by the programmer. Like a local reference, a global reference ensures thatthe referenced object will not be garbage collected.

5.问: 怎样去创建一个global references ? 

   答:

global ref-erences are created by just one JNI function, NewGlobalRef  例如:

   d->audio_record = jni_env->NewGlobalRef(d->audio_record);

6.问:怎样去释放Local references ?

   答:(*env)->DeleteLocalRef(env, jstr);

7.问:怎样去释放Global references?

   答: DeleteGlobalRef




声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。